C# 如何将serializedObject从编辑器脚本传递到编辑器窗口脚本? 使用系统集合; 使用System.Collections.Generic; 使用UnityEditor; 使用UnityEditorInternal; 使用UnityEngine; [CustomEditor(typeof(ConversationTrigger))] 公共类ConversationTriggerEditor:编辑器 { 公共会话编辑器窗口初始化器() { ConversationsEditorWindow myWindow=CreateInstance(); //Init(序列化对象); SerializedObject SeroObject=新的SerializedObject(myWindow); myWindow.Init(serobject); 返回我的窗口; } 公共覆盖无效OnInspectorGUI() { if(GUILayout.按钮(“配置项”)) { ConversationsEditorWindow myWindow=CreateInstance(); //Init(序列化对象); Init(序列化对象); } } }
我第一次在OnInspectorGUI中使用按钮时,它可以很好地使用该按钮。但是现在我不想在编辑器脚本中使用按钮,而是在编辑器窗口脚本中使用项目菜单 因此,我在编辑器脚本中创建了一个名为initser的方法 然后在编辑器窗口中:C# 如何将serializedObject从编辑器脚本传递到编辑器窗口脚本? 使用系统集合; 使用System.Collections.Generic; 使用UnityEditor; 使用UnityEditorInternal; 使用UnityEngine; [CustomEditor(typeof(ConversationTrigger))] 公共类ConversationTriggerEditor:编辑器 { 公共会话编辑器窗口初始化器() { ConversationsEditorWindow myWindow=CreateInstance(); //Init(序列化对象); SerializedObject SeroObject=新的SerializedObject(myWindow); myWindow.Init(serobject); 返回我的窗口; } 公共覆盖无效OnInspectorGUI() { if(GUILayout.按钮(“配置项”)) { ConversationsEditorWindow myWindow=CreateInstance(); //Init(序列化对象); Init(序列化对象); } } },c#,unity3d,C#,Unity3d,我第一次在OnInspectorGUI中使用按钮时,它可以很好地使用该按钮。但是现在我不想在编辑器脚本中使用按钮,而是在编辑器窗口脚本中使用项目菜单 因此,我在编辑器脚本中创建了一个名为initser的方法 然后在编辑器窗口中: using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEditorInternal; using UnityEngine; [CustomEd
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
[CustomEditor(typeof(ConversationTrigger))]
public class ConversationTriggerEditor : Editor
{
public ConversationsEditorWindow initser()
{
ConversationsEditorWindow myWindow = CreateInstance<ConversationsEditorWindow>();
//myWindow.Init(serializedObject);
SerializedObject serobject = new SerializedObject(myWindow);
myWindow.Init(serobject);
return myWindow;
}
public override void OnInspectorGUI()
{
if (GUILayout.Button("Configure Item"))
{
ConversationsEditorWindow myWindow = CreateInstance<ConversationsEditorWindow>();
//myWindow.Init(serializedObject);
myWindow.Init(serializedObject);
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEditor;
使用UnityEditorInternal;
使用UnityEngine;
公共类会话编辑器窗口:编辑器窗口
{
公共静态SerializedObject对话=null;
私有静态会话触发器_ConversationTrigger;
[SerializeField]私有静态ReorderableList会话列表;
私有静态序列化属性会话;
私有静态int_currentlySelectedConversationIndex=-1;
私有静态int newSize=0;
专用静态矢量2;
私有只读静态字典_dialoguesListDict=newdictionary();
私有只读静态字典_-sentencesListDict=新字典();
私有静态序列化对象itemcopy;
[菜单项(“对话系统/对话”)]
私有静态无效会话Swindow()
{
常数int宽度=800;
const int height=800;
var x=(Screen.currentResolution.width-width)/2;
变量y=(Screen.currentResolution.height-height)/2;
var window=GetWindow();
window.position=新矩形(x,y,宽度,高度);
ConversationTriggerEditor triggereditor=新的ConversationTriggerEditor();
triggereditor.initser();
}
公共void Init(序列化对象_项)
{
//复制项目targetObject,以便在创建时不会丢失引用
//单击“项目”窗口上的另一个图元。
itemcopy=新的序列化对象(_item.targetObject);
对话=项目副本;
//初始化窗口的其他事项
_conversationTrigger=(conversationTrigger)\u item.targetObject;
_conversations=itemcopy.FindProperty(“conversations”);
conversationsList=new ReorderableList(itemcopy,_conversations)
{
displayAdd=true,
displayRemove=true,
draggable=true,
drawHeaderCallback=DrawConversationsHeader,
DrawerElementCallback=DrawConversationsElement,
onAddCallback=(列表)=>
{
序列化属性添加;
//如果选择了某个元素,则在该元素之后添加,否则在末尾添加
如果(\u currentlySelectedConversationIndex>=0)
{
list.serializedProperty.InsertarrayElementIndex(_currentlySelectedConversationIndex+1);
addedElement=list.serializedProperty.GetArrayElementIndex(_currentlySelectedConversationIndex+1);
}
其他的
{
list.serializedProperty.arraySize++;
addedElement=list.serializedProperty.GetArrayElementIndex(list.serializedProperty.arraySize-1);
}
变量名称=addedElement.FindPropertyRelative(“名称”);
var foldout=addedElement.FindPropertyRelative(“foldout”);
var对话=addedElement.FindPropertyRelative(“对话”);
name.stringValue=“”;
foldout.boolValue=false;
dialogs.arraySize=0;
},
elementHeightCallback=(索引)=>
{
返回GetConversationHeight(_conversations.getArrayElementIndex(索引));
}
};
}
私有void OnGUI()
{
if(itemcopy!=null)
{
itemcopy.Update();
//如果没有元素重置_currentlySelectedConversationIndex
如果(conversationsList.serializedProperty.arraySize-1<\u currentlySelectedConversationIndex)\u currentlySelectedConversationIndex=-1;
现在conversationsList为空
在此之前,当我在编辑器脚本中使用一个按钮时,我在这里使用了一个没有菜单项的按钮,但没有使其成为静态的按钮,只是:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
public class ConversationsEditorWindow : EditorWindow
{
public static SerializedObject conversation = null;
private static ConversationTrigger _conversationTrigger;
[SerializeField] private static ReorderableList conversationsList;
private static SerializedProperty _conversations;
private static int _currentlySelectedConversationIndex = -1;
private static int newSize = 0;
private static Vector2 scrollPos;
private readonly static Dictionary<string, ReorderableList> _dialoguesListDict = new Dictionary<string, ReorderableList>();
private readonly static Dictionary<string, ReorderableList> _sentencesListDict = new Dictionary<string, ReorderableList>();
private static SerializedObject itemcopy;
[MenuItem("Dialog System/Conversations")]
private static void ConversationsWindow()
{
const int width = 800;
const int height = 800;
var x = (Screen.currentResolution.width - width) / 2;
var y = (Screen.currentResolution.height - height) / 2;
var window = GetWindow<ConversationsEditorWindow>();
window.position = new Rect(x, y, width, height);
ConversationTriggerEditor triggereditor = new ConversationTriggerEditor();
triggereditor.initser();
}
public void Init(SerializedObject _item)
{
// Copy the Item targetObject to not lose reference when you
// click another element on the project window.
itemcopy = new SerializedObject(_item.targetObject);
conversation = itemcopy;
// Other things to initialize the window
_conversationTrigger = (ConversationTrigger)_item.targetObject;
_conversations = itemcopy.FindProperty("conversations");
conversationsList = new ReorderableList(itemcopy, _conversations)
{
displayAdd = true,
displayRemove = true,
draggable = true,
drawHeaderCallback = DrawConversationsHeader,
drawElementCallback = DrawConversationsElement,
onAddCallback = (list) =>
{
SerializedProperty addedElement;
// if something is selected add after that element otherwise on the end
if (_currentlySelectedConversationIndex >= 0)
{
list.serializedProperty.InsertArrayElementAtIndex(_currentlySelectedConversationIndex + 1);
addedElement = list.serializedProperty.GetArrayElementAtIndex(_currentlySelectedConversationIndex + 1);
}
else
{
list.serializedProperty.arraySize++;
addedElement = list.serializedProperty.GetArrayElementAtIndex(list.serializedProperty.arraySize - 1);
}
var name = addedElement.FindPropertyRelative("Name");
var foldout = addedElement.FindPropertyRelative("Foldout");
var dialogues = addedElement.FindPropertyRelative("Dialogues");
name.stringValue = "";
foldout.boolValue = false;
dialogues.arraySize = 0;
},
elementHeightCallback = (index) =>
{
return GetConversationHeight(_conversations.GetArrayElementAtIndex(index));
}
};
}
private void OnGUI()
{
if (itemcopy != null)
{
itemcopy.Update();
// if there are no elements reset _currentlySelectedConversationIndex
if (conversationsList.serializedProperty.arraySize - 1 < _currentlySelectedConversationIndex) _currentlySelectedConversationIndex = -1;
使用系统集合;
使用System.Collections.Generic;
使用UnityEditor;
使用UnityEditorInternal;
使用UnityEngine;
公共类会话编辑器窗口:编辑器窗口
{
公共静态SerializedObject对话=null;
私有静态会话触发器_ConversationTrigger;
[SerializeField]私有静态ReorderableList会话列表;
私有静态序列化属性会话;
私有静态int_currentlySelectedConversationIndex=-1;
私有静态int newSize=0;
专用静态矢量2;
私有只读静态字典_dialoguesListDict=newdictionary();
私有只读静态字典_-sentencesListDict=新字典();
私有静态序列化对象itemcopy;
公共void Init(序列化对象_项)
{
//复制项目targetObject,以便在创建时不会丢失引用
//单击“项目”窗口上的另一个图元。
itemcopy=新系列
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
public class ConversationsEditorWindow : EditorWindow
{
public static SerializedObject conversation = null;
private static ConversationTrigger _conversationTrigger;
[SerializeField] private static ReorderableList conversationsList;
private static SerializedProperty _conversations;
private static int _currentlySelectedConversationIndex = -1;
private static int newSize = 0;
private static Vector2 scrollPos;
private readonly static Dictionary<string, ReorderableList> _dialoguesListDict = new Dictionary<string, ReorderableList>();
private readonly static Dictionary<string, ReorderableList> _sentencesListDict = new Dictionary<string, ReorderableList>();
private static SerializedObject itemcopy;
public void Init(SerializedObject _item)
{
// Copy the Item targetObject to not lose reference when you
// click another element on the project window.
itemcopy = new SerializedObject(_item.targetObject);
conversation = itemcopy;
// Other things to initialize the window
const int width = 800;
const int height = 800;
var x = (Screen.currentResolution.width - width) / 2;
var y = (Screen.currentResolution.height - height) / 2;
var window = GetWindow<ConversationsEditorWindow>();
window.position = new Rect(x, y, width, height);
_conversationTrigger = (ConversationTrigger)_item.targetObject;
_conversations = itemcopy.FindProperty("conversations");
conversationsList = new ReorderableList(itemcopy, _conversations)
{
displayAdd = true,
displayRemove = true,
draggable = true,
drawHeaderCallback = DrawConversationsHeader,
drawElementCallback = DrawConversationsElement,
onAddCallback = (list) =>
{
SerializedProperty addedElement;
// if something is selected add after that element otherwise on the end
if (_currentlySelectedConversationIndex >= 0)
{
list.serializedProperty.InsertArrayElementAtIndex(_currentlySelectedConversationIndex + 1);
addedElement = list.serializedProperty.GetArrayElementAtIndex(_currentlySelectedConversationIndex + 1);
}
else
{
list.serializedProperty.arraySize++;
addedElement = list.serializedProperty.GetArrayElementAtIndex(list.serializedProperty.arraySize - 1);
}
var name = addedElement.FindPropertyRelative("Name");
var foldout = addedElement.FindPropertyRelative("Foldout");
var dialogues = addedElement.FindPropertyRelative("Dialogues");
name.stringValue = "";
foldout.boolValue = false;
dialogues.arraySize = 0;
},
elementHeightCallback = (index) =>
{
return GetConversationHeight(_conversations.GetArrayElementAtIndex(index));
}
};
}
ConversationTriggerEditor triggereditor = new ConversationTriggerEditor();
var conversationTrigger = FindObjectOfType<ConversationTrigger>();
var conversationTriggerSerializedObject = new SerializedObject(conversationTrigger);
conversationTriggerSerializedObject.Update();
// ...
conversationTriggerSerializedObject.ApplyModifiedProperties();