public GenericDictionary<Button, GameObject> buttonPrefabMap; private GameObject currentPrefab; private bool isPrefabLoaded = false; public GameObject CHild;//子物体 public void LoadPrefab(Button button) { if (isPrefabLoaded) { Destroy(currentPrefab); isPrefabLoaded = false; return; } // 获取按钮对应的预制体 GameObject prefab = buttonPrefabMap[button];//GameObject // 销毁当前加载的预制体 // Destroy(currentPrefab); // 加载新的预制体 currentPrefab = Instantiate(prefab,CHild.transform); isPrefabLoaded = true; } unity 修改代码 增加点击一下按钮添加模型增加外轮廓红色 再次点击删除模型取消外轮廓
时间: 2024-02-11 21:26:36 浏览: 49
Unity3D脚本隐藏GameObject子物体1
下面是修改后的代码,实现了点击按钮添加模型并增加外轮廓红色,再次点击删除模型并取消外轮廓的功能:
public GenericDictionary<Button, GameObject> buttonPrefabMap;
private GameObject currentPrefab;
private bool isPrefabLoaded = false;
public GameObject CHild;
private Outline currentOutline;
public void LoadPrefab(Button button)
{
if (isPrefabLoaded)
{
Destroy(currentPrefab);
currentOutline.enabled = false;
isPrefabLoaded = false;
return;
}
// 获取按钮对应的预制体
GameObject prefab = buttonPrefabMap[button];
// 加载新的预制体
currentPrefab = Instantiate(prefab, CHild.transform);
isPrefabLoaded = true;
// 添加外轮廓组件并设置为红色
currentOutline = currentPrefab.AddComponent<Outline>();
currentOutline.OutlineMode = Outline.Mode.OutlineAll;
currentOutline.OutlineColor = Color.red;
}
// 点击按钮时调用 LoadPrefab 函数
public void OnButtonClick(Button button)
{
LoadPrefab(button);
}
阅读全文