C# 如何将存储的组件属性值粘贴到另一个项目,以及如何将属性格式化为字符串[]?
在一个项目中,我有一个带有副本的编辑器窗口脚本:C# 如何将存储的组件属性值粘贴到另一个项目,以及如何将属性格式化为字符串[]?,c#,unity3d,C#,Unity3d,在一个项目中,我有一个带有副本的编辑器窗口脚本: using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; using System.Reflection; using UnityEditorInternal; using System.Text; using System.Linq; public class EditorWindowCopyCompone
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.Reflection;
using UnityEditorInternal;
using System.Text;
using System.Linq;
public class EditorWindowCopyComponentsData : EditorWindow
{
[MenuItem("GameObject/Copy Components", false, 20)]
public static void CopyComponents(MenuCommand command)
{
var go = Selection.activeGameObject;//command.context as GameObject;
var components = go.GetComponents<Component>().ToList();
foreach (Transform child in go.transform)
{
foreach (Component component in child.GetComponents<Component>())
{
components.Add(component);
}
}
var serializedData = new string[components.Count];
for (int i = 0; i < components.Count; i++)
{
// Type-AssemblyQualifiedName : Json-Serialized-Data
serializedData[i] = components[i].GetType().AssemblyQualifiedName + ":" + EditorJsonUtility.ToJson(components[i]);
}
EditorGUIUtility.systemCopyBuffer = string.Join("\n", serializedData);
}
}
等等。。。
然后
这种格式就像它在组件中一样
要添加一个选项,还可以将子属性添加到Copybuffer或格式中,例如,在其左侧的属性EuleRanges上有一个小箭头,如果将鼠标放在该箭头上,它将显示一些更深层次的属性和值,以便在复制组件时在Copybuffer中设置格式以及如何设置这些属性和值
第二个问题是在其他项目中制作粘贴时:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.Reflection;
using UnityEditorInternal;
using System;
public class EditorWindowCopyComponentsData : EditorWindow
{
[MenuItem("GameObject/Paste Components", false, 21)]
public static void PasteComponents(MenuCommand command)
{
var go = Selection.activeGameObject;//command.context as GameObject;
var serializedData = EditorGUIUtility.systemCopyBuffer.Split('\n');
char[] splitter = { ':' };
foreach (var data in serializedData)
{
var typeAndJson = data.Split(splitter, 2);
var type = Type.GetType(typeAndJson[0]);
if (type.FullName == "UnityEngine.Transform") // only 1 transform
{
EditorJsonUtility.FromJsonOverwrite(typeAndJson[1], go.transform);
}
else
{
EditorJsonUtility.FromJsonOverwrite(typeAndJson[1], go.AddComponent(type));
}
}
}
}
复制时,它不会将child组件作为“go”的child。
在“复制”中,我也在子组件上循环,并将它们添加到组件列表中,然后也复制它们:
foreach (Transform child in go.transform)
{
foreach (Component component in child.GetComponents<Component>())
{
components.Add(component);
}
}
foreach(在go.Transform中转换子对象)
{
foreach(child.GetComponents()中的组件)
{
组件。添加(组件);
}
}
但是,我如何才能添加它们在相同的顺序和地点作为儿童也粘贴它
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.Reflection;
using UnityEditorInternal;
using System;
public class EditorWindowCopyComponentsData : EditorWindow
{
[MenuItem("GameObject/Paste Components", false, 21)]
public static void PasteComponents(MenuCommand command)
{
var go = Selection.activeGameObject;//command.context as GameObject;
var serializedData = EditorGUIUtility.systemCopyBuffer.Split('\n');
char[] splitter = { ':' };
foreach (var data in serializedData)
{
var typeAndJson = data.Split(splitter, 2);
var type = Type.GetType(typeAndJson[0]);
if (type.FullName == "UnityEngine.Transform") // only 1 transform
{
EditorJsonUtility.FromJsonOverwrite(typeAndJson[1], go.transform);
}
else
{
EditorJsonUtility.FromJsonOverwrite(typeAndJson[1], go.AddComponent(type));
}
}
}
}
foreach (Transform child in go.transform)
{
foreach (Component component in child.GetComponents<Component>())
{
components.Add(component);
}
}