Guest User

Untitled

a guest
Feb 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [Tiled2Unity.CustomTiledImporter]
  6. public class WraithNathCustomTiledImporter : Tiled2Unity.ICustomTiledImporter
  7. {
  8.  
  9. public void HandleCustomProperties(UnityEngine.GameObject gameObject, IDictionary<string, string> props)
  10. {
  11. }
  12.  
  13. public void CustomizePrefab(GameObject prefab)
  14. {
  15. Tiled2Unity.TiledMap map = prefab.GetComponent<Tiled2Unity.TiledMap>();
  16.  
  17. Rect mapRect = map.GetMapRect();
  18. float depthPerLayer = -map.TileHeight / mapRect.height;
  19.  
  20. var renderers = prefab.GetComponentsInChildren<Renderer>();
  21.  
  22. foreach (Renderer renderer in renderers)
  23. {
  24. //Get the transform for the parent layer
  25. Transform parentTransform = renderer.transform.parent.transform;
  26. float z = (depthPerLayer * SortingLayer.GetLayerValueFromID(renderer.sortingLayerID));
  27.  
  28. //Set the z position for the layer
  29. parentTransform.position = new Vector3(parentTransform.position.x, parentTransform.position.y, z); ;
  30. }
  31. }
  32. }
  33.  
  34. using System;
  35. using System.Collections;
  36. using System.Collections.Generic;
  37. using UnityEngine;
  38.  
  39. public class SpriteDepthInSortingLayer : MonoBehaviour {
  40.  
  41. [Tooltip("The TiledMap instance our sprite is interacting with.")]
  42. public Tiled2Unity.TiledMap AttachedMap = null;
  43.  
  44. [HideInInspector]
  45. [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.")]
  46. public int InteractWithSortingLayer = 0;
  47.  
  48. [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.")]
  49. public int TilesetHeight = 0;
  50.  
  51. private void Start()
  52. {
  53. if (this.AttachedMap == null)
  54. {
  55. 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."));
  56. return;
  57. }
  58. }
  59.  
  60. private void Update()
  61. {
  62. UpdateSpriteDepth();
  63. }
  64.  
  65. public void UpdateSpriteDepth()
  66. {
  67. // Put position into map space
  68. Vector3 spritePosition = this.gameObject.transform.position;
  69. spritePosition -= this.AttachedMap.gameObject.transform.position;
  70.  
  71. // 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.
  72. if (TilesetHeight != 0)
  73. {
  74. int delta_y = this.AttachedMap.TileHeight - this.TilesetHeight;
  75. spritePosition.y += delta_y;
  76. }
  77.  
  78. Rect mapRect = this.AttachedMap.GetMapRect();
  79. float depthPerLayer = -this.AttachedMap.TileHeight / mapRect.height;
  80.  
  81. float depth_z = (spritePosition.y / this.AttachedMap.ExportScale / mapRect.height) + (depthPerLayer * this.InteractWithSortingLayer);
  82.  
  83. // Assign our depth value in the z component.
  84. this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y, depth_z);
  85. }
  86.  
  87. }
  88.  
  89. using System.Collections;
  90. using System.Collections.Generic;
  91. using UnityEngine;
  92. using UnityEditor;
  93. using System;
  94. using System.Reflection;
  95.  
  96. [CustomEditor(typeof(SpriteDepthInSortingLayer))]
  97. public class SpriteDepthInSortingLayerEditor : UnityEditor.Editor
  98. {
  99.  
  100. public override void OnInspectorGUI()
  101. {
  102. DrawDefaultInspector();
  103.  
  104. // Get the renderer from the target object
  105. var script = (target as SpriteDepthInSortingLayer);
  106.  
  107. // If there is no script we cant do
  108. if (script==null)
  109. return;
  110.  
  111. //Get the current sort layer selection
  112. int sortLayerSelection = script.InteractWithSortingLayer;
  113.  
  114. GUIContent[] sortingLayerContexts = GetSortingLayerContexts();
  115. int newSortingLayerIndex = EditorGUILayout.Popup(new GUIContent("Sorting Layer"), sortLayerSelection, sortingLayerContexts);
  116.  
  117. //Change layer or show layers menu
  118. if (newSortingLayerIndex == sortingLayerContexts.Length - 1)
  119. {
  120. EditorApplication.ExecuteMenuItem("Edit/Project Settings/Tags and Layers");
  121. }
  122. else if (newSortingLayerIndex != sortLayerSelection)
  123. {
  124. Undo.RecordObject(script, "Edit Sorting Layer ID");
  125. script.InteractWithSortingLayer = newSortingLayerIndex;
  126. EditorUtility.SetDirty(script);
  127. }
  128.  
  129. //Add button to set the depth now
  130. if (GUILayout.Button("Set Depth (Changes Transform Z Position)"))
  131. {
  132. if (script.AttachedMap == null)
  133. {
  134. Debug.LogError("Cannot set sprite depth without an Attached Map");
  135. }
  136. else
  137. {
  138. script.UpdateSpriteDepth();
  139. }
  140. }
  141. }
  142.  
  143. public static string[] GetSortingLayerNames()
  144. {
  145. Type internalEditorUtilityType = typeof(UnityEditorInternal.InternalEditorUtility);
  146. PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
  147. return (string[])sortingLayersProperty.GetValue(null, new object[0]);
  148. }
  149.  
  150. public static GUIContent[] GetSortingLayerContexts()
  151. {
  152. List<GUIContent> contexts = new List<GUIContent>();
  153.  
  154. foreach (string layerName in GetSortingLayerNames())
  155. {
  156. contexts.Add(new GUIContent(layerName));
  157. }
  158.  
  159. contexts.Add(GUIContent.none);
  160. contexts.Add(new GUIContent("Edit Layers..."));
  161.  
  162. return contexts.ToArray();
  163. }
  164. }
Add Comment
Please, Sign In to add comment