ConformingSpline.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.U2D;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8. // Demo Script Usage:
  9. // When you want multiple SpriteShapes to share a common Spline,
  10. // attach this script to the secondary objects you would like to
  11. // copy the Spline and set the ParentObject to the original object
  12. // you are copying from.
  13. [ExecuteInEditMode]
  14. public class ConformingSpline : MonoBehaviour
  15. {
  16. public GameObject m_ParentObject;
  17. private int hashCode;
  18. // Use this for initialization
  19. void Start()
  20. {
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. if (m_ParentObject != null)
  26. {
  27. hashCode = CopySpline(m_ParentObject, gameObject, hashCode);
  28. }
  29. }
  30. private static int CopySpline(GameObject src, GameObject dst, int hashCode)
  31. {
  32. #if UNITY_EDITOR
  33. var parentSpriteShapeController = src.GetComponent<SpriteShapeController>();
  34. var mirrorSpriteShapeController = dst.GetComponent<SpriteShapeController>();
  35. if (parentSpriteShapeController != null && mirrorSpriteShapeController != null && parentSpriteShapeController.spline.GetHashCode() != hashCode)
  36. {
  37. SerializedObject srcController = new SerializedObject(parentSpriteShapeController);
  38. SerializedObject dstController = new SerializedObject(mirrorSpriteShapeController);
  39. SerializedProperty srcSpline = srcController.FindProperty("m_Spline");
  40. dstController.CopyFromSerializedProperty(srcSpline);
  41. dstController.ApplyModifiedProperties();
  42. EditorUtility.SetDirty(mirrorSpriteShapeController);
  43. return parentSpriteShapeController.spline.GetHashCode();
  44. }
  45. #endif
  46. return hashCode;
  47. }
  48. }