范围 无法正常工作
Range Not working correctly
我正在尝试在我的 int 前面使用 [Range(1f,3f)] 统一,这样我就可以在我的检查器中得到一个滑块。但它给出了一个错误 "The namespace name Range could not be found. Are you missing a using directive or assembly reference"。对于每个示例,我发现没有人与我有什么不同,可能是因为我使用的是旧版本的 Unity?我怎样才能在这个版本中让它成为可能还是不可能?
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class AssignMaterial : MonoBehaviour {
public bool assignMaterial;
[Range(1, 3)] public int matId;
void Update()
{
if(assignMaterial)
{
switch(matId)
{
case 1:
this.renderer.material = Resources.Load("NodeMaterial" + matId.ToString()) as Material;
break;
case 2:
this.renderer.material = Resources.Load("NodeMaterial" + matId.ToString()) as Material;
break;
case 3:
this.renderer.material = Resources.Load("NodeMaterial" + matId.ToString()) as Material;
break;
}
DestroyImmediate(this);
}
}
}
我认为这可能是您使用的版本不支持的情况。
我可以通过实现的范围属性确认自己的最低版本是 unity 4.2
除了范围属性之外,您还可以制作一个编辑器脚本。用一行这样的话:
AssignMaterial.matID = (int)EditorGUILayout.Slider(AssignMaterial.matID, 0, 3);
小边注
你可能想在里面放一些 try/catches。就像现在一样,如果没有附加到对象的渲染器。您会暂时冻结 unity,但最好捕获此错误并在未分配时处理该进程。
我正在尝试在我的 int 前面使用 [Range(1f,3f)] 统一,这样我就可以在我的检查器中得到一个滑块。但它给出了一个错误 "The namespace name Range could not be found. Are you missing a using directive or assembly reference"。对于每个示例,我发现没有人与我有什么不同,可能是因为我使用的是旧版本的 Unity?我怎样才能在这个版本中让它成为可能还是不可能?
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class AssignMaterial : MonoBehaviour {
public bool assignMaterial;
[Range(1, 3)] public int matId;
void Update()
{
if(assignMaterial)
{
switch(matId)
{
case 1:
this.renderer.material = Resources.Load("NodeMaterial" + matId.ToString()) as Material;
break;
case 2:
this.renderer.material = Resources.Load("NodeMaterial" + matId.ToString()) as Material;
break;
case 3:
this.renderer.material = Resources.Load("NodeMaterial" + matId.ToString()) as Material;
break;
}
DestroyImmediate(this);
}
}
}
我认为这可能是您使用的版本不支持的情况。 我可以通过实现的范围属性确认自己的最低版本是 unity 4.2
除了范围属性之外,您还可以制作一个编辑器脚本。用一行这样的话:
AssignMaterial.matID = (int)EditorGUILayout.Slider(AssignMaterial.matID, 0, 3);
小边注
你可能想在里面放一些 try/catches。就像现在一样,如果没有附加到对象的渲染器。您会暂时冻结 unity,但最好捕获此错误并在未分配时处理该进程。