20.Lua和C#的判空相关

20.Lua调用CSharp-判空


20.1 知识点

准备工作

启动Lua脚本

LuaManager.Instance.Init();
LuaManager.Instance.DoLuaFile("Lesson20_Lua调用CSharp_判空");

在Lua脚本创建测试对象并尝试得到一些不存在的组件

--声明一些Unity类的别名 方便xLua使用
GameObject = CS.UnityEngine.GameObject
Debug = CS.UnityEngine.Debug
Rigidbody = CS.UnityEngine.Rigidbody
Image = CS.UnityEngine.UI.Image

--创建测试对象
local testGameObject = GameObject("测试对象")

--尝试得到一些测试对象上的组件 其实都是得不到的 但是在Lua打印出的值可能为nil也可能为null
local rigidbody = testGameObject:GetComponent(typeof(Rigidbody))
print(rigidbody)--LUA: null: 0
Debug.Log(rigidbody)--null
local image = testGameObject:GetComponent(typeof(Image))
print(image)--LUA: nil
Debug.Log(image)--Null

==运算符判空

  • ==运算符判空 存在null和nil并不相等的问题 不能准确判空
--rigidbody == nil为false 不会进入打印
if rigidbody == nil then
    print("加组件测试的游戏对象的rigidbody是空")
end

--image == nil为true 会进入打印
if image == nil then
    print("加组件测试的游戏对象的image是空")
end

万物之父的Equals方法判空

  • 万物之父的Equals方法判空 在nil对象调用Equals方法时可能存在报错的问题
--rigidbody:Equals(nil)为true 会进入打印
if rigidbody:Equals(nil) then
    print("加组件测试的游戏对象的rigidbody是空")
end

-- --报错 image是nil 无法调用Equals方法
-- if image:Equals(nil) then
-- 	print("加组件测试的游戏对象的image是空")--
-- end

万物之父扩展判空方法判空

C#脚本中为Object拓展判空方法

//为Object 拓展判空方法
[LuaCallCSharp]
public static class Lesson20_Test
{
    //拓展一个为Object判空的方法 主要是给lua用 lua没法用null和nil比较
    public static bool IsNull(this UnityEngine.Object obj)
    {
        return obj == null;
    }
}

Lua脚本中使用拓展判空方法

  • Object 扩展的 IsNull 判空:当 Lua 变量为 真·nil 时无法调用实例方法,仍会报错
--rigidbody:IsNull()为true 会进入打印
if rigidbody:IsNull() then
    print("加组件测试的游戏对象的rigidbody是空")
end

-- --报错 image是nil 无法调用拓展判空方法
-- if image:IsNull() then
-- 	print("加组件测试的游戏对象的image是空")
-- end

自定义判空函数判空

  • 自定义判空函数 只要 ==运算符 或 万物之父的Equals方法 或 万物之父拓展判空方法 判空成立 就是空
function IsNull(obj)
    if obj == nil or obj:Equals(nil) or obj:IsNull() then
        return true
    end
    return false
end

--IsNull(rigidbody)为true 会进入打印
if IsNull(rigidbody) then
    print("加组件测试的游戏对象的rigidbody是空")
end

--IsNull(image)为true 会进入打印
if IsNull(image) then
    print("加组件测试的游戏对象的image是空")
end

20.2 知识点代码

Lesson20_Lua调用CSharp_判空.lua

print('我是Lua脚本 Lesson20_Lua调用CSharp_判空')

--声明一些Unity类的别名 方便xLua使用
GameObject = CS.UnityEngine.GameObject
Debug = CS.UnityEngine.Debug
Rigidbody = CS.UnityEngine.Rigidbody
Image = CS.UnityEngine.UI.Image

--创建测试对象
local testGameObject = GameObject("测试对象")

--尝试得到一些测试对象上的组件 其实都是得不到的 但是在Lua打印出的值可能为nil也可能为null
local rigidbody = testGameObject:GetComponent(typeof(Rigidbody))
print(rigidbody)--LUA: null: 0
Debug.Log(rigidbody)--null
local image = testGameObject:GetComponent(typeof(Image))
print(image)--LUA: nil
Debug.Log(image)--Null


print("**********知识点一 ==运算符判空************")
--==运算符判空 存在null和nil并不相等的问题 不能准确判空

--rigidbody == nil为false 不会进入打印
if rigidbody == nil then
    print("加组件测试的游戏对象的rigidbody是空")
end

--image == nil为true 会进入打印
if image == nil then
    print("加组件测试的游戏对象的image是空")
end


print("**********知识点二 万物之父的Equals方法判空************")
--万物之父的Equals方法判空 在nil对象调用Equals方法时 可能存在报错的问题 

--rigidbody:Equals(nil)为true 会进入打印
if rigidbody:Equals(nil) then
    print("加组件测试的游戏对象的rigidbody是空")
end

-- --报错 image是nil 无法调用Equals方法
-- if image:Equals(nil) then
-- 	print("加组件测试的游戏对象的image是空")--
-- end


print("**********知识点三 万物之父拓展判空方法判空************")
--Object 扩展 IsNull:obj 为真 nil 时无法调用实例方法 

--rigidbody:IsNull()为true 会进入打印
if rigidbody:IsNull() then
    print("加组件测试的游戏对象的rigidbody是空")
end

-- --报错 image是nil 无法调用拓展判空方法
-- if image:IsNull() then
-- 	print("加组件测试的游戏对象的image是空")
-- end

print("**********知识点四 自定义判空函数判空************")
--自定义判空函数 只要 ==运算符 或 万物之父的Equals方法 或 万物之父拓展判空方法 判空成立 就是空
function IsNull(obj)
    if obj == nil or obj:Equals(nil) or obj:IsNull() then
        return true
    end
    return false
end

--IsNull(rigidbody)为true 会进入打印
if IsNull(rigidbody) then
    print("加组件测试的游戏对象的rigidbody是空")
end

--IsNull(image)为true 会进入打印
if IsNull(image) then
    print("加组件测试的游戏对象的image是空")
end

Lesson20_Lua调用CSharp_判空

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

public class Lesson20_Lua调用CSharp_判空 : MonoBehaviour
{
    void Start()
    {
        LuaManager.Instance.Init();
        LuaManager.Instance.DoLuaFile("Lesson20_Lua调用CSharp_判空");
    }
}

//为Object 拓展判空方法
[LuaCallCSharp]
public static class Lesson20_Test
{
    //拓展一个为Object判空的方法 主要是给lua用 lua没法用null和nil比较
    public static bool IsNull(this UnityEngine.Object obj)
    {
        return obj == null;
    }
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏