Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make custom inspector add object reference in Unity

I cannot figure out how to make a texture field appear in my custom inspector and the documentation explains little to nothing on how to do it.

I have a script that has an Icon field with a texture type in my main script however need to write it into a custom inspector. Please explain how I could do this. My main script is stored as ItemReference so that I can access it's variables.

I have left what I have below so you can see where I am up to in re-writing inspector.

using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(Item))]
public class ItemCustomEditor : Editor {

    //IMPORTANT!! TWEAKING THIS SCRIPT WITHOUT KNOWLEDGE OF THE UNITY EDITOR API MAY BREAK FUNCTIONS OF OTHER SCRIPTS IN GAME SUCH AS MAKING ALL VARIABLES INVISIBLE.
    public override void OnInspectorGUI(){
        base.OnInspectorGUI();
        Item ItemReference = (Item)target;
        EditorGUILayout.Separator();
        EditorGUILayout.Separator();
        ItemReference.ID = EditorGUILayout.IntField("Item ID", ItemReference.ID);

    }
}

That is the editor script and here is the normal script that the editor script refers to.

    using UnityEngine;
using System.Collections;

public class Item : MonoBehaviour {
    //First we declare variables that the database will use
    public int ID;
    public Texture icon;
    //Drop and use buttons can be turned off by setting value to #000 this is not recommended for the Use Button as user will not be able to use items; however can come in handy 
    //If you want to ban dropping items in cutscenes or certain areas to set that up use a trigger and set the string to #000 to void the command.
    public string dropButton;
    public string useButton;
    public bool autoDestruct;
    public bool clickToCollect;
    public bool enterToCollect;
    void Update(){
        // Automatic human error detection system detects invalid characters in fields that would usually break system and removes the problem to ensure smooth operation.
        // This system is set up and does not require the user to tweak setting; ONLY MODIFY IF YOU ARE PROFFICIENT IN C#.
            if (useButton.Length > 1){
                useButton = "#000";
                Debug.LogError ("UseButton command may not contain more than 1 letter or number please shorten your command; Your use button has been disabled.");
            }
            else if (useButton.Contains("!") || useButton.Contains("@") || useButton.Contains ("%") || useButton.Contains ("^")){
                useButton = "#000";
                Debug.LogError ("UseButton command must not contain symbols please edit your command.");
            }
            if (useButton.Contains ("&") || useButton.Contains ("*") || useButton.Contains ("(") || useButton.Contains (")") || useButton.Contains ("-") || useButton.Contains ("_")) {
                useButton = "#000";
                Debug.LogError ("UseButton command must not contain symbols please edit your command.");
            }
            if (useButton.Contains ("/") || useButton.Contains ("+") || useButton.Contains ("=") || useButton.Contains (".") || useButton.Contains (",") || useButton.Contains ("?")) {
                useButton = "#000";
                Debug.LogError ("UseButton command must not contain symbols please edit your command.");
            }
            if (useButton.Contains (">") || useButton.Contains ("<") || useButton.Contains ("~") || useButton.Contains ("`")) {
                useButton = "#000";
                Debug.LogError ("UseButton command must not contain symbols please edit your command.");
            }
            if (useButton == "" || dropButton == "" || icon == null){
                Debug.LogWarning("Please ensure all items in the ITEMDB script are assigned you are missing values for 1 or more variables. As is some functionalities may not work as expected");
            }
            if (clickToCollect == true && enterToCollect == true){
                Debug.LogError("You have checked both click to collect and enter to collect options in: " + this.gameObject.name + "'s ItemDB script please ensure only one is selected.");
            }
        }
    }
like image 779
Luke Avatar asked Jan 10 '15 10:01

Luke


People also ask

How do I find GameObject references in Unity?

From the Editor, go to the Project Tab, select the given Asset, right-click on it and then click Find References In Scene. It will show you every GameObject that the given Asset is attached to in the Hierarchy View if the given Asset is a script.

How do I get script references in Unity?

To use it, grab the code from here (it needs to be in under a folder named “Editor”). Then you can right click an object or script in the project window and click “Find References”. It'll pop up a window with all the prefabs, which you can select from there, or click the little arrow to dig deeper. EASY!

Where do editors put scripts?

To use the CustomEditor attribute, you must place your script inside an Editor folder, or in a folder nested inside an Editor folder.


1 Answers

What you do is use an ObjectField and specify the required type (Texture).

Here is some sample code for a custom editor for an integer and Texture property on a MonoBehaviour:

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(MyScript))]
public class MyScriptEditor : Editor {

    public override void OnInspectorGUI()
    {
        var script = (MyScript) target;

        script.someProperty = EditorGUILayout.IntField("A value", script.someProperty);
        script.texture = (Texture) EditorGUILayout.ObjectField("Image", script.texture, typeof (Texture), false);
    }
}
like image 80
Paul-Jan Avatar answered Sep 25 '22 07:09

Paul-Jan