在运行时编辑后使用预制件生成新的预制件



我有简单的脚本和完美的工作,但只能在 Unity 编辑器中运行。我尝试为移动设备构建,不完整且错误构建。我使用 Unity 2019.3.4。 是否有移动脚本的替代"使用 UnityEditor"。

我需要移动脚本。在移动设备上运行的任何想法。

using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.Collections.Generic;
public class Prefabz : MonoBehaviour
{
public GameObject selObj;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
CreatePrefab();
}
}
void CreatePrefab()
{
//GameObject[] selObjs = Selection.gameObjects;
string charName = selObj.name;
// duplicate
GameObject newInstance = Instantiate(selObj);
newInstance.name = charName;
// now replace the prefab
string prefabPath = "Assets/trideeScript/" + charName + ".prefab";
var existingPrefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));
if (existingPrefab != null)
{
PrefabUtility.ReplacePrefab(newInstance, existingPrefab, ReplacePrefabOptions.ReplaceNameBased);
}
else
{
PrefabUtility.CreatePrefab(prefabPath, newInstance);
}
// delete dupe
DestroyImmediate(newInstance);
Debug.Log("Prefab'd " + charName + "! "" + prefabPath + """);
}   
}

UnityEditor 命名空间在任何平台上都不受支持,除了实际的编辑器。这就是为什么所有编辑器脚本都必须放在"编辑器"文件夹中的原因,这就是为什么将任何与编辑器相关的东西保存在预处理器指令中是一种很好的做法#if UNITY_EDITOR

最新更新