Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Create Object in SceneView through Inspector

Discussion in 'Scripting' started by BigBGros, Apr 1, 2014.

  1. BigBGros

    BigBGros

    Joined:
    Aug 30, 2013
    Posts:
    21
    Hi all,

    I have this simple code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ViewObjectInSceneView : MonoBehaviour {
    6.  
    7.     public GameObject SomeObject;
    8.  
    9.     void Start () {
    10.        
    11.     }
    12.    
    13.  
    14.     void Update () {
    15.    
    16.     }
    17. }
    18.  
    19.  
    $Inspector.jpg

    And here is where I get stuck. I want to make a custom Inspector that, when I drag a gameobject(prefab) and drop into the GameObject field in the Inspector of the script above, automatically generates and show this object in the SceneView. Is there any way to do that?

    Thanks in Advance.
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    This doesn't sound awfully efficient, but here is one way you could achieve this.

    Updated behaviour script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. #if UNITY_EDITOR
    5. using UnityEditor;
    6. #endif
    7.  
    8. [ExecuteInEditMode]
    9. public class ViewObjectInSceneView : MonoBehaviour {
    10.     public GameObject someObjectPrefab;
    11.  
    12. #if UNITY_EDITOR
    13.     private GameObject _someObjectEditorInstance;
    14.  
    15.     public void RefreshEditorPreviewInstance() {
    16.         if (_someObjectEditorInstance != null)
    17.             Object.DestroyImmediate(_someObjectEditorInstance);
    18.  
    19.         _someObjectEditorInstance = EditorUtility.InstantiatePrefab(someObjectPrefab) as GameObject;
    20.         _someObjectEditorInstance.hideFlags = HideFlags.HideAndDontSave;
    21.  
    22. // ADDED - the position probably wants to be initialized from this transform!
    23.         _someObjectEditorInstance.transform.position = transform.position;
    24.     }
    25.  
    26.     void Awake() {
    27.         RefreshEditorPreviewInstance();
    28.     }
    29. #endif
    30.  
    31.     void Start () {
    32.  
    33.     }
    34.  
    35.     void Update () {
    36.  
    37.     }
    38. }
    39.  
    The editor script counterpart:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(ViewObjectInSceneView))]
    6. [CanEditMultipleObjects]
    7. public class ViewObjectInSceneViewEditor : Editor {
    8.     private SerializedProperty someObjectProp;
    9.  
    10.     private void OnEnable() {
    11.         someObjectProp = serializedObject.FindProperty("someObjectProp");
    12.     }
    13.  
    14.     public override void OnInspectorGUI() {
    15.         serializedObject.Update();
    16.  
    17.         EditorGUI.BeginChangeCheck();
    18.         EditorGUILayout.PropertyField(someObjectProp, new GUIContent("Some Object"));
    19.         if (EditorGUI.EndChangeCheck()) {
    20.             foreach (var t in targets)
    21.                 (t as ViewObjectInSceneView).RefreshEditorPreviewInstance();
    22.         }
    23.  
    24.         serializedObject.ApplyModifiedProperties();
    25.     }
    26. }
    27.  
    Warning, I haven't tested the above, but I think that it should work :)
     
    Last edited: Apr 1, 2014
  3. BigBGros

    BigBGros

    Joined:
    Aug 30, 2013
    Posts:
    21
    Thanks a lot numberkruncher... I'll try the editor script counterpart and tell you something
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    The "correct" way to do this (better, cleaner, less flaky than ExecuteInEditMode) would be to write a custom inspector for your class.
     
  5. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I think that you probably need both since the custom inspector is only executed when an object becomes selected. Whereas, ExecuteInEditMode should cause object previews to appear immediately each time the scene is loaded.

    The better option might be to render the in-editor preview using OnDrawGizmos... but that of course is a little more involved.
     
  6. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I added an extra line below the prefab instantiation part since you probably want to initialize the position of the preview instance to the position of your object itself.
     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Code (csharp):
    1.  
    2. [ExecuteInEditMode]
    3. public class RandomSpawner : MonoBehaviour
    4. {
    5.     public List<GameObject> prefabs = new List<GameObject>();
    6.  
    7. #if UNITY_EDITOR
    8.     private void Update()
    9.     {
    10.         foreach (GameObject prefab in prefabs)
    11.         {
    12.             if (prefab == null)
    13.                 continue;
    14.  
    15.             MeshFilter filter = prefab.GetComponent<MeshFilter>();
    16.             MeshRenderer renderer = prefab.GetComponent<MeshRenderer>();
    17.  
    18.             if (filter != null  renderer != null  filter.sharedMesh != null)
    19.                 Graphics.DrawMesh(filter.sharedMesh, transform.position, transform.rotation, renderer.sharedMaterial, 0);
    20.         }
    21.     }
    22. #endif
    23. }
    24.  
     
  8. BigBGros

    BigBGros

    Joined:
    Aug 30, 2013
    Posts:
    21
    I tried LightStriker method and it works... but seems to slowdown Editor Tasks... i would prefer a custom inspector... is possible to do it using a custom inspector? how?

    Thanks
     
  9. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    My example demonstrates how to do what you asked with a custom inspector.
     
  10. BigBGros

    BigBGros

    Joined:
    Aug 30, 2013
    Posts:
    21
    Thanks numberkruncher... it works perfect!!!