解析其他脚本中prefab(UILabel)的值
Parsing the value of prefab(UILabel) in other script
我想知道如何访问预制件的值。例如,我在场景中创建了一个 ngui 进度条,并显示百分比允许进度条的位置。进度条是预制的,我想使用 'GetComponent' 访问其他脚本中的百分比值。
这是 uiroot Strat() 方法的脚本:
protected virtual void Start ()
{
// Here - > Showing Null Exception
UILabel uiLabel = GetComponent<UILabel> ();
Debug.Log (uiLabel.text);
// end
UIOrthoCamera oc = GetComponentInChildren<UIOrthoCamera>();
if (oc != null)
{
Debug.LogWarning("UIRoot should not be active at the same time as UIOrthoCamera. Disabling UIOrthoCamera.", oc);
Camera cam = oc.gameObject.GetComponent<Camera>();
oc.enabled = false;
if (cam != null) cam.orthographicSize = 1f;
}
else Update();
}
这是UILabel(预制)的脚本:
public void SetCurrentPercent ()
{
if (UIProgressBar.current != null)
text = Mathf.RoundToInt(UIProgressBar.current.value * 100f) + "%";
// I want to access this text!!!
Debug.Log (text);
}
如何在某处访问 'text'?
两种情况都出现错误。
1.
Assets/NGUI/Scripts/UI/UIRoot.cs(151,64): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
Assets/NGUI/Scripts/UI/UIRoot.cs(151,52): error CS1502: The best overloaded method match for `UnityEngine.Object.Instantiate(UnityEngine.Object)' has some invalid arguments
Assets/NGUI/Scripts/UI/UIRoot.cs(151,52): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Object'
2.
Assets/NGUI/Scripts/UI/UIRoot.cs(153,35): error CS0176: Static member `UILabel.value' cannot be accessed with an instance reference, qualify it with a type name instead
您可以实例化预制件以从实例中读取值。
您的脚本只需要保存对预制件的引用。
public UILabel prefab; // assign this in the inspector
protected virtual void Start()
{
Debug.Log(prefab.text);
...
}
要分配预制件(使其不为空),请单击场景中的 UIRoot 脚本,然后将预制件从 "Project" 选项卡拖动到 "Inspector" 选项卡上的预制件插槽.
抱歉反馈晚了。
我收到以下错误。
错误 CS0029:无法隐式转换类型 string' to
UnityEngine.GameObject'
我意识到我必须调用 parent 的 child。
Object 的预制件有 child 我想获得的组件。
总之,我很容易想通。这是一个很简单的问题。
下面是一段脚本。
<script>
UILabel UILabel = GameObject.Find ("third").GetComponentInChildren<UILabel> ();
print (UILabel.text);
我想知道如何访问预制件的值。例如,我在场景中创建了一个 ngui 进度条,并显示百分比允许进度条的位置。进度条是预制的,我想使用 'GetComponent' 访问其他脚本中的百分比值。
这是 uiroot Strat() 方法的脚本:
protected virtual void Start ()
{
// Here - > Showing Null Exception
UILabel uiLabel = GetComponent<UILabel> ();
Debug.Log (uiLabel.text);
// end
UIOrthoCamera oc = GetComponentInChildren<UIOrthoCamera>();
if (oc != null)
{
Debug.LogWarning("UIRoot should not be active at the same time as UIOrthoCamera. Disabling UIOrthoCamera.", oc);
Camera cam = oc.gameObject.GetComponent<Camera>();
oc.enabled = false;
if (cam != null) cam.orthographicSize = 1f;
}
else Update();
}
这是UILabel(预制)的脚本:
public void SetCurrentPercent ()
{
if (UIProgressBar.current != null)
text = Mathf.RoundToInt(UIProgressBar.current.value * 100f) + "%";
// I want to access this text!!!
Debug.Log (text);
}
如何在某处访问 'text'?
两种情况都出现错误。
1.
Assets/NGUI/Scripts/UI/UIRoot.cs(151,64): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
Assets/NGUI/Scripts/UI/UIRoot.cs(151,52): error CS1502: The best overloaded method match for `UnityEngine.Object.Instantiate(UnityEngine.Object)' has some invalid arguments
Assets/NGUI/Scripts/UI/UIRoot.cs(151,52): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Object'
2.
Assets/NGUI/Scripts/UI/UIRoot.cs(153,35): error CS0176: Static member `UILabel.value' cannot be accessed with an instance reference, qualify it with a type name instead
您可以实例化预制件以从实例中读取值。
您的脚本只需要保存对预制件的引用。
public UILabel prefab; // assign this in the inspector
protected virtual void Start()
{
Debug.Log(prefab.text);
...
}
要分配预制件(使其不为空),请单击场景中的 UIRoot 脚本,然后将预制件从 "Project" 选项卡拖动到 "Inspector" 选项卡上的预制件插槽.
抱歉反馈晚了。
我收到以下错误。
错误 CS0029:无法隐式转换类型 string' to
UnityEngine.GameObject'
我意识到我必须调用 parent 的 child。 Object 的预制件有 child 我想获得的组件。 总之,我很容易想通。这是一个很简单的问题。
下面是一段脚本。
<script>
UILabel UILabel = GameObject.Find ("third").GetComponentInChildren<UILabel> ();
print (UILabel.text);