xLua
xLua copied to clipboard
关于UnityEnging.Object==null的建议
因为faq教程中提到了这个问题,所以我只是想提一个建议,加在教程里。
可惜的是,如果没有按照教程,没有预先配置一个IsNull()
方法,那这个问题在Lua里写代码就很尴尬了。
经过我尝试,可以用以下代码解决,我这里版本为2019.4 ,其他Unity多种版本还没测试
参考Unity引擎源码为: https://github.com/Unity-Technologies/UnityCsReference/blob/2019.4/Runtime/Export/Scripting/UnityEngineObject.bindings.cs
public override bool Equals(object other)
{
Object otherAsObject = other as Object;
// A UnityEngine.Object can only be equal to another UnityEngine.Object - or null if it has been destroyed.
// Make sure other is a UnityEngine.Object if "as Object" fails. The explicit "is" check is required since the == operator
// in this class treats destroyed objects as equal to null
if (otherAsObject == null && other != null && !(other is Object))
return false;
return CompareBaseObjects(this, otherAsObject);
}
public static bool operator==(Object x, Object y) { return CompareBaseObjects(x, y); }
也就是说可以Equals
正好调用了==
重载的判断方法CompareBaseObjects
则可以用Equals来做判断
if self.image ~= nil and self.image:Equals(nil) == false then
---
end
我记得Equals不一定行
Equals有的时候并不能进行判空
if(self.m_Panel:GetComponent(typeof(CS.UnityEngine.Canvas)):Equals(nil)) then
m_Panel上并没有Canvas组件但返回的为false
我不觉得@570189458你的案例有什么问题