using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIManager : MonoBehaviour
{
// 用户点击图标按钮,克隆对应模型到GroundPlane子物体下面
public GameObject EarthObj;
public GameObject MarsObj;
public GameObject MeRcuryObj;
public GameObject ParentOBJ;
public void ClickEarthIcon()
{
if (ParentOBJ.transform.childCount == 0)
{
Debug.Log("点击了地球图标,开始克隆地球");
GameObject.Instantiate(EarthObj, ParentOBJ.transform);
}
else
{
Debug.Log("开始清空所有子物体");
ClearChild();
GameObject.Instantiate(EarthObj, ParentOBJ.transform);
}
}
public void ClickMarsIcon()
{
Debug.Log("点击了火星图标,开始克隆火星");
if (ParentOBJ.transform.childCount == 0)
{
Debug.Log("点击了地球图标,开始克隆地球");
GameObject.Instantiate(MarsObj, ParentOBJ.transform);
}
else
{
ClearChild();
GameObject.Instantiate(MarsObj, ParentOBJ.transform);
}
}
public void ClickMercuryIcon()
{
if (ParentOBJ.transform.childCount == 0)
{
Debug.Log("点击了水星图标,开始克隆水星");
GameObject.Instantiate(MeRcuryObj, ParentOBJ.transform);
}
else
{
ClearChild();
GameObject.Instantiate(MeRcuryObj, ParentOBJ.transform);
}
}
void ClearChild()
{
Transform[] TempChildTrans = ParentOBJ.transform.GetComponentsInChildren<Transform>();
for (int i = 1; i < TempChildTrans.Length; i++)
{
Destroy(TempChildTrans[i].gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIManager : MonoBehaviour
{
// 用户点击图标按钮,克隆对应模型到GroundPlane子物体下面
public GameObject EarthObj;
public GameObject MarsObj;
public GameObject MeRcuryObj;
public GameObject ParentOBJ;
public void ClickEarthIcon()
{
CloneChildToFather(EarthObj);
}
public void ClickMarsIcon()
{
Debug.Log("点击了某个图标,开始克隆");
CloneChildToFather(MarsObj);
}
public void ClickMercuryIcon()
{
CloneChildToFather(MeRcuryObj);//调用克隆函数传递形参
}
void CloneChildToFather(GameObject OriginalClone)
{
if (ParentOBJ.transform.childCount == 0)
{
GameObject.Instantiate(OriginalClone, ParentOBJ.transform);
}
else
{
ClearChild();
GameObject.Instantiate(OriginalClone, ParentOBJ.transform);
}
}
/// <summary>
/// 清除子物体
/// </summary>
void ClearChild()
{
Transform[] TempChildTrans = ParentOBJ.transform.GetComponentsInChildren<Transform>();
for (int i = 1; i < TempChildTrans.Length; i++)
{
Destroy(TempChildTrans[i].gameObject);
}
}
}