![](/img/trans.png)
[英]How can I add a option to a GameObject in the Hierarchy context menu?
[英]How can I add enum menu to gameobject?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InteractableItem : MonoBehaviour
{
enum InteractableMode // your custom enumeration
{
Item1,
Item2,
Item3
};
InteractableMode interactableMode = InteractableMode.Item1;
public float distance = 10f;
[TextArea(1,10)]
public string description = "";
}
我可以在檢查器中看到距離和描述字段,但是當我將腳本附加到游戲對象時,我看不到枚舉菜單。
我想查看枚舉菜單並能夠在運行游戲之前查看 select 和更改菜單中的模式。 這個想法是能夠決定兩個選項之一,如果游戲對象是可交互的信息項或也可以作為拾取。
如果選擇了信息模式,則像現在一樣使用它,但如果選擇了 Pickup,則立即使用它,並且還添加/使用一個動作方法進行拾取。
為了讓它顯示在編輯器中,Unity 必須能夠序列化interactableMode
。 為此,您可以將其公開(然后您還必須公開InteractableMode
枚舉),或者您可以強制 Unity 通過執行以下操作對其進行序列化:
[SerializeField]
InteractableMode interactableMode = InteractableMode.Item1;
嘗試以下操作,即public
並實例化它:
public class InteractableItem : MonoBehaviour
{
public InteractableMode interactableMode = InteractableMode.Item1;
public enum InteractableMode // your custom enumeration
{
Item1,
Item2,
Item3
};
public float distance = 10f;
[TextArea(1,10)]
public string description = "";
}
將 enum 設為 public 並將字段設為 public 或 [SerializedField] 設為私有:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InteractableItem : MonoBehaviour
{
public enum InteractableMode // your custom enumeration
{
Item1,
Item2,
Item3
};
[SerializeField]
private InteractableMode interactableMode = InteractableMode.Item1;
public float distance = 10f;
[TextArea(1, 10)]
public string description = "";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.