Unity琐碎(1) 编辑器参数修改

本文介绍了解决Unity编辑器中面板参数变化无法实时反映到场景表现的方法。提供了两种解决方案:使用MonoBehaviour.OnValidate()方法和自定义编辑器。此外还提到了使用[ExecuteInEditMode]属性来确保代码逻辑在编辑器模式下正确执行。

今天在写编辑器面板的时候,突然发现如果面板参数变化的时候,不能实时修改表现效果(参数没有生效)。

public int monsterCount  ;
void Awake() 
{
    monsterCount = Mathf.Clamp(monsterCount, 0, 1000);
}

就像上面的代码,只在启动的时候的会修改变量的数值,运行中修改编辑器中参数不会再去执行Awake的....

解决思路

(1) MonoBehavior.OnValidate()

his function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).

Use this function to validate the data of your MonoBehaviours. This can be used to ensure that when you modify date in an editor that the data stays within a certain range.

(2) 自定义编辑器
在自定义编辑器中访问更新数据相关的接口(代码不准确,意思表达清晰即可)

public int monsterCount  ;
public int MonsterCount
{
    set{monsterCount   = value ; UpdateParam() ;}
}

void Awake() 
{
    monsterCount = Mathf.Clamp(monsterCount, 0, 1000);
}

void UpdateParam()
{
    monsterCount = Mathf.Clamp(monsterCount, 0, 1000);
}
附加

有些代码逻辑在Awake中不实现的,也可以尝试定义[[ExecuteInEditMode ]]解决(https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html)解决编辑器下的问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值