/**************************************************************************** * Copyright 2019 Nreal Techonology Limited. All rights reserved. * * This file is part of NRSDK. * * https://www.nreal.ai/ * *****************************************************************************/ namespace NRKernal.NREditor { using System.Collections.Generic; using UnityEngine; using UnityEditor; /// A nr serialized trackable. public class NRSerializedTrackable { /// The serialized object. protected readonly SerializedObject m_SerializedObject; /// Name of the trackable. private readonly SerializedProperty m_TrackableName; /// The initialized in editor. private readonly SerializedProperty m_InitializedInEditor; /// Gets the serialized object. /// The serialized object. public SerializedObject SerializedObject { get { return m_SerializedObject; } } /// Gets the trackable name property. /// The trackable name property. public SerializedProperty TrackableNameProperty { get { return m_TrackableName; } } /// Gets or sets the name of the trackable. /// The name of the trackable. public string TrackableName { get { return m_TrackableName.stringValue; } set { m_TrackableName.stringValue = value; } } /// Gets the initialize in editor preperty. /// The initialize in editor preperty. public SerializedProperty InitInEditorPreperty { get { return m_InitializedInEditor; } } /// Gets or sets a value indicating whether the initialized in editor. /// True if initialized in editor, false if not. public bool InitializedInEditor { get { return m_InitializedInEditor.boolValue; } set { m_InitializedInEditor.boolValue = value; } } /// Constructor. /// Target for the. public NRSerializedTrackable(SerializedObject target) { m_SerializedObject = target; m_TrackableName = m_SerializedObject.FindProperty("m_TrackableName"); m_InitializedInEditor = m_SerializedObject.FindProperty("m_InitializedInEditor"); } /// Gets the material. /// The material. public Material GetMaterial() { return ((MonoBehaviour)m_SerializedObject.targetObject).GetComponent().sharedMaterial; } /// Gets the materials. /// An array of material. public Material[] GetMaterials() { return ((MonoBehaviour)m_SerializedObject.targetObject).GetComponent().sharedMaterials; } /// Sets a material. /// The material. public void SetMaterial(Material material) { object[] targetObjs = m_SerializedObject.targetObjects; for (int i = 0; i < targetObjs.Length; i++) { ((MonoBehaviour)targetObjs[i]).GetComponent().sharedMaterial = material; } NREditorSceneManager.Instance.UnloadUnusedAssets(); } /// Sets a material. /// The materials. public void SetMaterial(Material[] materials) { object[] targetObjs = m_SerializedObject.targetObjects; for (int i = 0; i < targetObjs.Length; i++) { ((MonoBehaviour)targetObjs[i]).GetComponent().sharedMaterials = materials; } NREditorSceneManager.Instance.UnloadUnusedAssets(); } /// Gets game objects. /// The game objects. public List GetGameObjects() { List list = new List(); object[] targetObjs = m_SerializedObject.targetObjects; for (int i = 0; i < targetObjs.Length; i++) { list.Add(((MonoBehaviour)targetObjs[i]).gameObject); } return list; } } }