Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [Tiled2Unity.CustomTiledImporter]
- public class WraithNathCustomTiledImporter : Tiled2Unity.ICustomTiledImporter
- {
- public void HandleCustomProperties(UnityEngine.GameObject gameObject, IDictionary<string, string> props)
- {
- }
- public void CustomizePrefab(GameObject prefab)
- {
- Tiled2Unity.TiledMap map = prefab.GetComponent<Tiled2Unity.TiledMap>();
- Rect mapRect = map.GetMapRect();
- float depthPerLayer = -map.TileHeight / mapRect.height;
- var renderers = prefab.GetComponentsInChildren<Renderer>();
- foreach (Renderer renderer in renderers)
- {
- //Get the transform for the parent layer
- Transform parentTransform = renderer.transform.parent.transform;
- float z = (depthPerLayer * SortingLayer.GetLayerValueFromID(renderer.sortingLayerID));
- //Set the z position for the layer
- parentTransform.position = new Vector3(parentTransform.position.x, parentTransform.position.y, z); ;
- }
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class SpriteDepthInSortingLayer : MonoBehaviour {
- [Tooltip("The TiledMap instance our sprite is interacting with.")]
- public Tiled2Unity.TiledMap AttachedMap = null;
- [HideInInspector]
- [Tooltip("Which Sorting layer our sprite is interacting with. Will render above lower layers and below higher layers. Render order of Tiles on same layer will depend on location.")]
- public int InteractWithSortingLayer = 0;
- [Tooltip("For maps where tileset heights are different than map tile heights. Enter the tileset height here. Useful/crucial for isometric maps. Leave at default (0) if you don't care.")]
- public int TilesetHeight = 0;
- private void Start()
- {
- if (this.AttachedMap == null)
- {
- Debug.LogError(String.Format("Sprite must be attached to a TiledMap instance in order to calucluate the 'z-depth' on that map. Check the SpriteDepthInMap component in the Inspector."));
- return;
- }
- }
- private void Update()
- {
- UpdateSpriteDepth();
- }
- public void UpdateSpriteDepth()
- {
- // Put position into map space
- Vector3 spritePosition = this.gameObject.transform.position;
- spritePosition -= this.AttachedMap.gameObject.transform.position;
- // Some maps (like isometric) have a tileset height that is larger than the map tile height in order to get the isometric illusion. We need to know that difference in caluclating depth.
- if (TilesetHeight != 0)
- {
- int delta_y = this.AttachedMap.TileHeight - this.TilesetHeight;
- spritePosition.y += delta_y;
- }
- Rect mapRect = this.AttachedMap.GetMapRect();
- float depthPerLayer = -this.AttachedMap.TileHeight / mapRect.height;
- float depth_z = (spritePosition.y / this.AttachedMap.ExportScale / mapRect.height) + (depthPerLayer * this.InteractWithSortingLayer);
- // Assign our depth value in the z component.
- this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y, depth_z);
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using System;
- using System.Reflection;
- [CustomEditor(typeof(SpriteDepthInSortingLayer))]
- public class SpriteDepthInSortingLayerEditor : UnityEditor.Editor
- {
- public override void OnInspectorGUI()
- {
- DrawDefaultInspector();
- // Get the renderer from the target object
- var script = (target as SpriteDepthInSortingLayer);
- // If there is no script we cant do
- if (script==null)
- return;
- //Get the current sort layer selection
- int sortLayerSelection = script.InteractWithSortingLayer;
- GUIContent[] sortingLayerContexts = GetSortingLayerContexts();
- int newSortingLayerIndex = EditorGUILayout.Popup(new GUIContent("Sorting Layer"), sortLayerSelection, sortingLayerContexts);
- //Change layer or show layers menu
- if (newSortingLayerIndex == sortingLayerContexts.Length - 1)
- {
- EditorApplication.ExecuteMenuItem("Edit/Project Settings/Tags and Layers");
- }
- else if (newSortingLayerIndex != sortLayerSelection)
- {
- Undo.RecordObject(script, "Edit Sorting Layer ID");
- script.InteractWithSortingLayer = newSortingLayerIndex;
- EditorUtility.SetDirty(script);
- }
- //Add button to set the depth now
- if (GUILayout.Button("Set Depth (Changes Transform Z Position)"))
- {
- if (script.AttachedMap == null)
- {
- Debug.LogError("Cannot set sprite depth without an Attached Map");
- }
- else
- {
- script.UpdateSpriteDepth();
- }
- }
- }
- public static string[] GetSortingLayerNames()
- {
- Type internalEditorUtilityType = typeof(UnityEditorInternal.InternalEditorUtility);
- PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
- return (string[])sortingLayersProperty.GetValue(null, new object[0]);
- }
- public static GUIContent[] GetSortingLayerContexts()
- {
- List<GUIContent> contexts = new List<GUIContent>();
- foreach (string layerName in GetSortingLayerNames())
- {
- contexts.Add(new GUIContent(layerName));
- }
- contexts.Add(GUIContent.none);
- contexts.Add(new GUIContent("Edit Layers..."));
- return contexts.ToArray();
- }
- }
Add Comment
Please, Sign In to add comment