unity3D ngui 插件 关于隐藏和显示游戏对象的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity3D ngui 插件 关于隐藏和显示游戏对象的问题相关的知识,希望对你有一定的参考价值。

我使用了如下代码显示和隐藏游戏对象,使用UIBUTTONMESSAGE脚本

using UnityEngine;
using System.Collections;

public class ButtonCtrl : MonoBehaviour

public GameObject MenuObject;

// Use this for initialization
void Start ()
MenuObject = GameObject.Find("MenuObject");


// Update is called once per frame
void Update ()



void Hide()
//Label_test = gameObject.GetComponent<UILabel>();
//Label_test.name = "Label_test";
//Label_test.text = "Can show!中文字幕";

//LabelDisable = gameObject.GetComponent<UILabel>();
//LabelDisable.name = "LabelDisable";

NGUITools.SetActive(MenuObject, false);


void Show()

NGUITools.SetActive(MenuObject, true);



可以隐藏,但是隐藏之后无法显示了,是函数NGUITools.SetActive的问题么,有什么好的解决办法?

参考技术A 我看了下NGUI关于 NGUITools.SetActive(gameobject,state)这个方法的定义,是这样定义的
static public void SetActive (GameObject go, bool state)

if (state)

Activate(go.transform);

else

Deactivate(go.transform);



然后Activae(Transform t)是这样定义的:

static void Activate (Transform t)

SetActiveSelf(t.gameObject, true);

// Prior to Unity 4, active state was not nested. It was possible to have an enabled child of a disabled object.
// Unity 4 onwards made it so that the state is nested, and a disabled parent results in a disabled child.
#if UNITY_3_5
for (int i = 0, imax = t.GetChildCount(); i < imax; ++i)

Transform child = t.GetChild(i);
Activate(child);

#else
// If there is even a single enabled child, then we're using a Unity 4.0-based nested active state scheme.
for (int i = 0, imax = t.childCount; i < imax; ++i)

Transform child = t.GetChild(i);
if (child.gameObject.activeSelf) return;


// If this point is reached, then all the children are disabled, so we must be using a Unity 3.5-based active state scheme.
for (int i = 0, imax = t.childCount; i < imax; ++i)

Transform child = t.GetChild(i);
Activate(child);

#endif


SetActiveSelf(t.gameObject, true);方法是这样定义的:
static public void SetActiveSelf(GameObject go, bool state)

#if UNITY_3_5
go.active = state;
#else
go.SetActive(state);
#endif


结论就是他的这个NGUITools.SetActive(gameObject, false);方法归根结底还是用的gameObject.setActivate(bool state)方法,所以你还是直接用gameObject.setActivate(bool state)方法就可以了
参考技术B 额 你说的隐藏其实就是将它摧毁了 你都摧毁了 怎么还能调用它呢?想调用就还要重新载入。
给你个提示吧 如果隐藏的话就赋予它一个空值 显示的时候再赋予图片的精灵 这样是最简单的方法。

以上是关于unity3D ngui 插件 关于隐藏和显示游戏对象的问题的主要内容,如果未能解决你的问题,请参考以下文章

Unity3D游戏开发NGUI之DrawCall数量

[Unity3D]Unity3D游戏开发之继续探索NGUI

unity3d、ngui插件做界面图片模糊

Unity3D插件NGUI基础学习笔记

002-unity3d插件使用

Unity3D的NGUI插件导入工程后提示错误:UnityEngine.QualitySettings、 GetQualityLevel