2015-01-10 25 views
2

我无法弄清楚如何使我的自定义检查器中出现纹理字段,文档解释如何做到这一点。如何使自定义检查器在Unity中添加对象引用

我有一个脚本,在我的主脚本中有一个带有纹理类型的图标字段,但是需要将它写入自定义检查器。请解释我如何做到这一点。我的主脚本存储为ItemReference,以便我可以访问它的变量。

我已经离开了我的下面,所以你可以看到我在重写检查员。

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); 

    } 
} 

这是编辑器脚本,这里是编辑器脚本引用的正常脚本。

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."); 
      } 
     } 
    } 

回答

4

你所做的是使用ObjectField并指定所需的类型(纹理)。

这里是一个整数和纹理属性自定义编辑器上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); 
    } 
} 
+0

注:d:d:d非常感谢你! – Luke

相关问题