FbxPrefab.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections.Generic;
  2. using System.Runtime.CompilerServices;
  3. using UnityEngine;
  4. [assembly: InternalsVisibleTo("Unity.Formats.Fbx.Editor")]
  5. [assembly: InternalsVisibleTo("Unity.Formats.Fbx.Editor.Tests")]
  6. [assembly: InternalsVisibleTo("Unity.ProBuilder.AddOns.Editor")]
  7. namespace UnityEngine.Formats.Fbx.Exporter
  8. {
  9. [System.Serializable]
  10. internal struct StringPair {
  11. private string m_fbxObjectName;
  12. public string FBXObjectName
  13. {
  14. get { return m_fbxObjectName; }
  15. set { m_fbxObjectName = value; }
  16. }
  17. private string m_unityObjectName;
  18. public string UnityObjectName
  19. {
  20. get { return m_unityObjectName; }
  21. set { m_unityObjectName = value; }
  22. }
  23. }
  24. /// <summary>
  25. /// Handler for an OnUpdate event.
  26. ///
  27. /// The update is performed on a temporary instance, which, shortly after
  28. /// this handler is invoked, will be applied to the prefab.
  29. ///
  30. /// The event handler can make changes to any objects in the hierarchy rooted
  31. /// by the updatedInstance. Those changes will be applied to the prefab.
  32. ///
  33. /// The updatedObjects include all objects in the temporary instance
  34. /// that were:
  35. /// - created, or
  36. /// - changed parent, or
  37. /// - had a component that was created, destroyed, or updated.
  38. /// There is no notification for entire objects that were destroyed.
  39. /// </summary>
  40. internal delegate void HandleUpdate(FbxPrefab updatedInstance, IEnumerable<GameObject> updatedObjects);
  41. /// <summary>
  42. /// This component is applied to a prefab. It keeps the prefab sync'd up
  43. /// with an FBX file.
  44. ///
  45. /// Other parts of the ecosystem:
  46. /// FbxPrefabInspector
  47. /// FbxPrefabAutoUpdater
  48. /// </summary>
  49. internal class FbxPrefab : MonoBehaviour
  50. {
  51. //////////////////////////////////////////////////////////////////////
  52. // TODO: Fields included in editor must be included in player, or it doesn't
  53. // build.
  54. /// <summary>
  55. /// Representation of the FBX file as it was when the prefab was
  56. /// last saved. This lets us update the prefab when the FBX changes.
  57. /// </summary>
  58. [SerializeField] // [HideInInspector]
  59. string m_fbxHistory;
  60. [SerializeField]
  61. List<StringPair> m_nameMapping = new List<StringPair>();
  62. /// <summary>
  63. /// Which FBX file does this refer to?
  64. /// </summary>
  65. [SerializeField]
  66. [Tooltip("Which FBX file does this refer to?")]
  67. GameObject m_fbxModel;
  68. /// <summary>
  69. /// Should we auto-update this prefab when the FBX file is updated?
  70. /// <summary>
  71. [Tooltip("Should we auto-update this prefab when the FBX file is updated?")]
  72. [SerializeField]
  73. bool m_autoUpdate = true;
  74. public string FbxHistory {
  75. get{
  76. return m_fbxHistory;
  77. }
  78. set{
  79. m_fbxHistory = value;
  80. }
  81. }
  82. public List<StringPair> NameMapping
  83. {
  84. get
  85. {
  86. return m_nameMapping;
  87. }
  88. }
  89. public GameObject FbxModel {
  90. get{
  91. return m_fbxModel;
  92. }
  93. set{
  94. m_fbxModel = value;
  95. }
  96. }
  97. public bool AutoUpdate {
  98. get{
  99. return m_autoUpdate;
  100. }
  101. set{
  102. m_autoUpdate = value;
  103. }
  104. }
  105. //////////////////////////////////////////////////////////////////////////
  106. // Event handling for updates.
  107. /// <summary>
  108. /// OnUpdate is raised once when an FbxPrefab gets updated, after all the changes
  109. /// have been done.
  110. /// </summary>
  111. public static event HandleUpdate OnUpdate;
  112. /// <summary>
  113. /// Notify listeners that they're free to make adjustments.
  114. /// This will be called after the FbxPrefab auto updater has completed it's work.
  115. /// </summary>
  116. /// <param name="instance">Updated FbxPrefab instance.</param>
  117. /// <param name="updatedObjects">Updated objects.</param>
  118. public static void CallOnUpdate(FbxPrefab instance, IEnumerable<GameObject> updatedObjects){
  119. if (OnUpdate != null) {
  120. OnUpdate (instance, updatedObjects);
  121. }
  122. }
  123. }
  124. }