93 lines
2.4 KiB
C#
93 lines
2.4 KiB
C#
using System;
|
||
using UnityEngine;
|
||
|
||
namespace PhxhSDK
|
||
{
|
||
/// <summary>
|
||
/// gameobject对象工厂,封装了gameobject的创建和销毁
|
||
/// </summary>
|
||
public static partial class Utils
|
||
{
|
||
private static Transform _defaultRoot;
|
||
|
||
|
||
public static GameObject CreateGameObject(bool dontDestroyOnLoad)
|
||
{
|
||
try
|
||
{
|
||
var go = new GameObject();
|
||
// if(dontDestroyOnLoad)GameObject.DontDestroyOnLoad(go);
|
||
go.transform.parent = GetDefaultRoot();
|
||
return go;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError(e);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public static void Destroy(GameObject go)
|
||
{
|
||
try
|
||
{
|
||
GameObject.Destroy(go);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError(e);
|
||
}
|
||
}
|
||
|
||
public static GameObject Clone(GameObject templateGo,GameObject parObj=null)
|
||
{
|
||
try
|
||
{
|
||
if (templateGo!=null)
|
||
{
|
||
Transform parent = null;
|
||
if (parObj != null && parObj.transform != null)
|
||
{
|
||
parent = parObj.transform;
|
||
}
|
||
|
||
if (parent == null)
|
||
{
|
||
parent = templateGo.transform.parent;
|
||
}
|
||
|
||
if (parent == null)
|
||
{
|
||
return GameObject.Instantiate(templateGo);
|
||
}
|
||
|
||
return GameObject.Instantiate(templateGo, parent, true);
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError(e);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
public static Transform GetDefaultRoot()
|
||
{
|
||
try
|
||
{
|
||
if (_defaultRoot == null)
|
||
{
|
||
GameObject gameObj = new GameObject("GameObjectFactoryRoot");
|
||
// Object.DontDestroyOnLoad(gameObj);
|
||
_defaultRoot = gameObj.transform;
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError(e);
|
||
}
|
||
return _defaultRoot;
|
||
}
|
||
}
|
||
} |