所以我有一個帶有一堆變數的類:
class DialogueBase
{
[SerializeField] private string input;
[SerializeField] private Color textColor;
[SerializeField] private float delay;
}
我想要另一個包含此類陣列的腳本,以便我可以添加一堆對話并在編輯器中編輯每個對話。例如,我需要在一個對話中包含 3 個對話,因此我將陣列的長度設為 3 并在編輯器中編輯每個值。
是否有可能做到這一點?還是我傻?
uj5u.com熱心網友回復:
正如 hijinxbassist 所說,您需要宣告您的類可序列化。
[System.Serializable]
class DialogueBase
{
[SerializeField] private string input;
[SerializeField] private Color textColor;
[SerializeField] private float delay;
}
然后
public DialoguBase[] dialoges;
用于檢查員編輯
還有一件額外的事情。您不需要使用private關鍵字來使您的欄位私有。根據 C# 檔案,欄位默認是私有的。所以private int A在字面上與int A.
uj5u.com熱心網友回復:
Muhammad Usama Alam 評論了我的問題,將我鏈接到另一個類似的帖子。但是在上述帖子中,他們已經解決了我的問題,并且正在嘗試解決另一個問題。鏈接在這里:stackoverflow.com/a/55407835/4812203
我的問題是(由 hijinxbassist 和 Cool guy 建議)我沒有使用[System.Serializable]. 但這并沒有解決我所有的問題。然后我不得不 [由 S.Hoseinpoor 建議] 使用List<>.
最后,[摘自 Muhammad Usama Alam 的評論] 我需要制作一個編輯器腳本,以使整個變數陣列顯示在編輯器上。}
讓它停下來:
這是我的包含對話行基礎的腳本:
[System.Serialible]
public class DialogueLine
{
[SerializeField] private string input;
[SerializeField] private Color textColor;
[SerializeField] private float delay;
}
然后我的 DialogueBase 腳本:
public class DialogueBaseClass : MonoBehaviour
{
[SerializeField]
public List<DialogueLine> DialogueSize = new List<DialogueLine>();
(...)
}
最后是我的編輯器腳本,以便編輯器按照我想要的方式呈現所有內容(直接從鏈接中提取,歸功于 Dubi Duboni)
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(DialogueBase))]
public class DialogueBase : Editor
{
private SerializedProperty _dialogues;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
_dialogues = serializedObject.FindProperty("dialogue");
serializedObject.Update();
for (int i = 0; i < _dialogues.arraySize; i )
{
var dialogue = _dialogues.GetArrayElementAtIndex(i);
EditorGUILayout.PropertyField(dialogue, new GUIContent("Dialogue " i));
}
}
}
Hope anyone who finds this gets what they need. Again, credit for that last part goes to the real author, I do not claim to be the original writter of that code. You can read the whole thread containing the last part here: stackoverflow.com/a/55407835/4812203 (Suggested by Muhammad Usama Alam).
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/317366.html
