美文网首页
【Unity插件】SetProperty -在Inspector

【Unity插件】SetProperty -在Inspector

作者: 絮大王 | 来源:发表于2018-05-18 23:56 被阅读0次

    转载请注明出处!

    更多插件翻译,可以看我的个人博客:http://blog.xudawang.fun

    介绍

    在使用Unity的时候,我们经常会把某些字段(变量)暴露在Inspector面板上,像是这样:

    如果我们想把属性暴露在Inspector面板上怎么办呢?

    这时我们就可以用到SetProperty“插件”啦!使用它,你只需要写一行超级简单的代码,就可以让属性出现在Inspector面板上啦!

    这个“插件”其实不是插件,这是LMNRY大神上传到Github上的一个开源项目。

    你可以免费下载,并使用这个“插件”!

    Github地址:https://github.com/LMNRY/SetProperty

    值得一提的是,这个插件是使用的MIT开源许可证,关于MIT许可证,大致意思如下:

    “MIT License

    MIT是和BSD一样宽松的许可协议,作者只想保留版权,而无任何其他了限制.也就是说,你必须在你的发行版里包含原许可协议的声明,无论你是以二进制发布的还是以源代码发布的。

    * 你可以使用,复制和修改软件

    * 你可以免费使用软件或出售

    * 唯一的限制是,它是必须附有MIT授权协议(如果是以源代码的形式提供就要在其中包含MIT的LICENSE文件,如果以软件的形式发布就要在界面显示说明(在使用了别人的代码部分注明出处、作者、使用了MIT协议即可))”

    作者:一代骑侠     原文链接:https://www.zhihu.com/question/25079718/answer/30025234    来源:知乎

    如何使用?

    第1步:从Github上下载文件

    首先打开这个网址:https://github.com/LMNRY/SetProperty

    先点击绿色的【Clone or download】按钮

    再点击【Download ZIP】,这样就可以下载下来文件啦!

    第2步:解压下载下来的文件

    文件下载下来是一个.zip的压缩包,直接解压就可以

    第3步:将解压好的文件,导入Unity工程

    1. 在Unity工程里新建一个文件夹,叫SetProperty (其实随便叫什么都可以)

    2. 然后把之前解压出来的所有的文件,都拖进刚刚建好的SetProperty文件夹中 (注意LICENSE文件就是MIT授权协议,请不要删除它)

    3. 导入完成!

    第4步:使用!

    1. 创建一个新的脚本,就叫做Test.cs吧!

    2. 在脚本中输入:

    [SerializeField, SetProperty("Number")] //这个特性就是此“插件”的核心,照着写就可以,参数中填写属性名。注意此特性要放在字段上!
    private float number;
    public float Number //这是我们要暴露在Inspector面板上的属性
    {
    get { return number; }
    private set { number = Mathf.Clamp01(value); }
    }

    3. 把脚本挂载到随便一个游戏物体上,即可看到效果!

    附录:SetProperty官方文档

    A PropertyAttribute/PropertyDrawer combination that allows for properties in Unity

    Example

    Unity allows you to serialize private fields with [SerializeField]. Use the new [SetProperty] attribute to have a public property set every time the field is modified in Unity's inspector. NOTE: It is okay to have private setters for public properties. Vanilla classes (i.e. non-MonoBehaviours) also work as well.

    [SerializeField, SetProperty("Number")]

    private float number;

    public float Number

    {

    get{return number;}

    private set{number = Mathf.Clamp01(value);}

    }

    相关文章

      网友评论

          本文标题:【Unity插件】SetProperty -在Inspector

          本文链接:https://www.haomeiwen.com/subject/jnondftx.html