unity2018编辑UI节点Bug问题解决方案
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;
using UnityEditor.Experimental.SceneManagement;
[InitializeOnLoad]
class CustomPrefabEnvironment
{
static CustomPrefabEnvironment()
{
// Uncomment this to dynamically create a plane when entering Prefab Mode
PrefabStage.prefabStageOpened += OnPrefabStageOpened;
}
static void OnPrefabStageOpened(PrefabStage prefabStage)
{
Debug.Log("OnPrefabStageOpened " + prefabStage.prefabAssetPath);
// Get info from the PrefabStage
var root = prefabStage.prefabContentsRoot;
var scene = prefabStage.scene;
GameObject sceneRootCanvas = null;
if (root.GetComponent<Canvas>() == null)
return;
var sceneRootObjs = scene.GetRootGameObjects();
for (int i = 0; i < sceneRootObjs.Length; i++)
{
var sceneRootObj = sceneRootObjs[i];
if (sceneRootObj == root)
continue;
if (sceneRootObj.GetComponent<Canvas>())
{
sceneRootCanvas = sceneRootObj;
break;
}
}
if (sceneRootCanvas)
{
root.transform.SetParent(sceneRootCanvas.transform);
GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(prefabStage.prefabAssetPath);
RectTransform rcTrans = root.GetComponent<RectTransform>();
RectTransform srcRcTrans = obj.GetComponent<RectTransform>();
rcTrans.localPosition = srcRcTrans.localPosition;
rcTrans.localScale = srcRcTrans.localScale;
rcTrans.localRotation = srcRcTrans.localRotation;
rcTrans.pivot = srcRcTrans.pivot;
rcTrans.anchorMin = srcRcTrans.anchorMin;
rcTrans.anchorMax = srcRcTrans.anchorMax;
rcTrans.offsetMax = srcRcTrans.offsetMax;
rcTrans.offsetMin = srcRcTrans.offsetMin;
rcTrans.anchoredPosition = srcRcTrans.anchoredPosition;
rcTrans.anchoredPosition3D = srcRcTrans.anchoredPosition3D;
rcTrans.sizeDelta = srcRcTrans.sizeDelta;
}
}
}
本文标题:unity2018编辑UI节点Bug问题解决方案
本文链接:https://www.haomeiwen.com/subject/amonbqtx.html
网友评论