NRSerializedTrackable.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.NREditor
  10. {
  11. using System.Collections.Generic;
  12. using UnityEngine;
  13. using UnityEditor;
  14. /// <summary> A nr serialized trackable. </summary>
  15. public class NRSerializedTrackable
  16. {
  17. /// <summary> The serialized object. </summary>
  18. protected readonly SerializedObject m_SerializedObject;
  19. /// <summary> Name of the trackable. </summary>
  20. private readonly SerializedProperty m_TrackableName;
  21. /// <summary> The initialized in editor. </summary>
  22. private readonly SerializedProperty m_InitializedInEditor;
  23. /// <summary> Gets the serialized object. </summary>
  24. /// <value> The serialized object. </value>
  25. public SerializedObject SerializedObject { get { return m_SerializedObject; } }
  26. /// <summary> Gets the trackable name property. </summary>
  27. /// <value> The trackable name property. </value>
  28. public SerializedProperty TrackableNameProperty { get { return m_TrackableName; } }
  29. /// <summary> Gets or sets the name of the trackable. </summary>
  30. /// <value> The name of the trackable. </value>
  31. public string TrackableName { get { return m_TrackableName.stringValue; } set { m_TrackableName.stringValue = value; } }
  32. /// <summary> Gets the initialize in editor preperty. </summary>
  33. /// <value> The initialize in editor preperty. </value>
  34. public SerializedProperty InitInEditorPreperty { get { return m_InitializedInEditor; } }
  35. /// <summary> Gets or sets a value indicating whether the initialized in editor. </summary>
  36. /// <value> True if initialized in editor, false if not. </value>
  37. public bool InitializedInEditor { get { return m_InitializedInEditor.boolValue; } set { m_InitializedInEditor.boolValue = value; } }
  38. /// <summary> Constructor. </summary>
  39. /// <param name="target"> Target for the.</param>
  40. public NRSerializedTrackable(SerializedObject target)
  41. {
  42. m_SerializedObject = target;
  43. m_TrackableName = m_SerializedObject.FindProperty("m_TrackableName");
  44. m_InitializedInEditor = m_SerializedObject.FindProperty("m_InitializedInEditor");
  45. }
  46. /// <summary> Gets the material. </summary>
  47. /// <returns> The material. </returns>
  48. public Material GetMaterial()
  49. {
  50. return ((MonoBehaviour)m_SerializedObject.targetObject).GetComponent<Renderer>().sharedMaterial;
  51. }
  52. /// <summary> Gets the materials. </summary>
  53. /// <returns> An array of material. </returns>
  54. public Material[] GetMaterials()
  55. {
  56. return ((MonoBehaviour)m_SerializedObject.targetObject).GetComponent<Renderer>().sharedMaterials;
  57. }
  58. /// <summary> Sets a material. </summary>
  59. /// <param name="material"> The material.</param>
  60. public void SetMaterial(Material material)
  61. {
  62. object[] targetObjs = m_SerializedObject.targetObjects;
  63. for (int i = 0; i < targetObjs.Length; i++)
  64. {
  65. ((MonoBehaviour)targetObjs[i]).GetComponent<Renderer>().sharedMaterial = material;
  66. }
  67. NREditorSceneManager.Instance.UnloadUnusedAssets();
  68. }
  69. /// <summary> Sets a material. </summary>
  70. /// <param name="materials"> The materials.</param>
  71. public void SetMaterial(Material[] materials)
  72. {
  73. object[] targetObjs = m_SerializedObject.targetObjects;
  74. for (int i = 0; i < targetObjs.Length; i++)
  75. {
  76. ((MonoBehaviour)targetObjs[i]).GetComponent<Renderer>().sharedMaterials = materials;
  77. }
  78. NREditorSceneManager.Instance.UnloadUnusedAssets();
  79. }
  80. /// <summary> Gets game objects. </summary>
  81. /// <returns> The game objects. </returns>
  82. public List<GameObject> GetGameObjects()
  83. {
  84. List<GameObject> list = new List<GameObject>();
  85. object[] targetObjs = m_SerializedObject.targetObjects;
  86. for (int i = 0; i < targetObjs.Length; i++)
  87. {
  88. list.Add(((MonoBehaviour)targetObjs[i]).gameObject);
  89. }
  90. return list;
  91. }
  92. }
  93. }