//--------------------------------------------------------
|
// [Author]: 第二世界
|
// [ Date ]: Tuesday, October 31, 2017
|
//--------------------------------------------------------
|
using UnityEngine;
|
using UnityEditor;
|
using System.Collections;
|
using UnityEngine.UI;
|
|
|
public class PrefabCreateTool
|
{
|
// [MenuItem("GameObject/UI/UIRoot")]
|
// public static UIRoot CreateUIRoot()
|
// {
|
// var uiroot = GameObject.FindObjectOfType<UIRoot>();
|
// if (uiroot == null)
|
// {
|
// var prefab = BuiltInLoader.LoadPrefab("UIRoot");
|
// var root = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity);
|
// root.name = "UIRoot";
|
// uiroot = root.GetComponent<UIRoot>();
|
// }
|
// return uiroot;
|
// }
|
|
public static void SetParent(GameObject instance, Transform parent = null)
|
{
|
if (instance == null)
|
return;
|
// var uiroot = CreateUIRoot();
|
if (parent == null)
|
if (Selection.transforms != null && Selection.transforms.Length > 0)
|
{
|
parent = Selection.transforms[0];
|
}
|
// else
|
// {
|
// parent = uiroot.normalCanvas.transform;
|
// }
|
instance.transform.SetParentEx(parent, Vector3.zero, Quaternion.identity, Vector3.one);
|
// PrefabUtility.DisconnectPrefabInstance(instance);
|
|
// 设置UI对象的Layer为UI
|
instance.layer = LayerMask.NameToLayer("UI");
|
|
// 递归设置所有子对象的Layer为UI
|
foreach (Transform child in instance.transform)
|
{
|
SetLayerRecursively(child.gameObject, "UI");
|
}
|
|
Selection.activeGameObject = instance;
|
}
|
|
// 递归设置GameObject及其所有子对象的Layer
|
private static void SetLayerRecursively(GameObject obj, string layerName)
|
{
|
if (obj == null)
|
return;
|
|
obj.layer = LayerMask.NameToLayer(layerName);
|
|
foreach (Transform child in obj.transform)
|
{
|
SetLayerRecursively(child.gameObject, layerName);
|
}
|
}
|
|
public static GameObject LoadUIPattern(string name)
|
{
|
return AssetDatabase.LoadAssetAtPath<GameObject>(string.Format("Assets/Editor/UIPattern/{0}.prefab", name));
|
}
|
|
[MenuItem("GameObject/UI/FunctionButton")]
|
public static void CreateFunctionButton()
|
{
|
var instance = UIUtility.CreateWidget("FunctionButtonPattern", "FunctionButton");
|
SetParent(instance);
|
}
|
|
[MenuItem("GameObject/UI/GeneralWin_1")]
|
public static void CreateWindowPattern1()
|
{
|
var prefab = LoadUIPattern("GeneralWin");
|
var instance = GameObject.Instantiate(prefab) as GameObject;
|
instance.name = "GeneralWin";
|
SetParent(instance);
|
}
|
|
[MenuItem("GameObject/UI/RewardGroup")]
|
public static void CreateRewardGroup()
|
{
|
var prefab = LoadUIPattern("Container_Rewards");
|
var instance = GameObject.Instantiate(prefab) as GameObject;
|
instance.name = "Container_Rewards";
|
SetParent(instance);
|
}
|
|
[MenuItem("GameObject/UI/自选商品")]
|
public static void CreateCommonItemCell()
|
{
|
var prefab = LoadUIPattern("CustomizedItemCell");
|
var instance = GameObject.Instantiate(prefab) as GameObject;
|
instance.name = "CustomizedItemCell";
|
SetParent(instance);
|
}
|
[MenuItem("GameObject/UI/特效")]
|
public static void CreateUIEffect()
|
{
|
var instance = new GameObject("UIEffect");
|
instance.AddComponent<UIEffect>();
|
instance.AddComponent<RectTransform>();
|
SetParent(instance);
|
}
|
|
[MenuItem("GameObject/UI/ItemCell")]
|
public static void CreateItemCell()
|
{
|
var button = UIUtility.CreateWidget("ItemCell", "ItemCell");
|
SetParent(button);
|
}
|
|
[MenuItem("GameObject/UI/SortTable")]
|
public static void CreateSortTable()
|
{
|
var prefab = LoadUIPattern("SortTable");
|
var instance = GameObject.Instantiate(prefab) as GameObject;
|
instance.name = "SortTable";
|
SetParent(instance);
|
}
|
|
[MenuItem("GameObject/UI/TextEx")]
|
public static void CreateTextEx()
|
{
|
var instance = new GameObject("TextEx");
|
var text = instance.AddComponent<TextEx>();
|
text.font = FontUtility.preferred;
|
text.fontSize = 22;
|
text.alignment = TextAnchor.MiddleCenter;
|
text.raycastTarget = false;
|
SetParent(instance);
|
}
|
|
[MenuItem("GameObject/UI/ImageEx")]
|
public static void CreateImageEx()
|
{
|
var instance = new GameObject("ImageEx");
|
var image = instance.AddComponent<ImageEx>();
|
image.raycastTarget = false;
|
SetParent(instance);
|
}
|
|
[MenuItem("GameObject/UI/通用按钮")]
|
public static void CreateButtonEx()
|
{
|
var button = UIUtility.CreateWidget("CommonButton", "CommonButton");
|
SetParent(button);
|
}
|
|
[MenuItem("GameObject/UI/红点")]
|
public static void CreateRedPoint()
|
{
|
var button = UIUtility.CreateWidget("RedPoint", "RedPoint");
|
SetParent(button);
|
}
|
|
[MenuItem("GameObject/UI/SecondFrameLoad")]
|
public static void CreateSecondFrameLoad()
|
{
|
var prefab = LoadUIPattern("SecondFrameLoad");
|
var instance = GameObject.Instantiate(prefab) as GameObject;
|
instance.name = "SecondFrameLoad";
|
SetParent(instance);
|
}
|
|
|
}
|