脚本是EditorWindow类型。
变量wasUndo是全局类型bool。我想使用此标志来帮助我决定何时启用对“撤消上一步”按钮的错误/正确设置。
问题在于它在OnGUI内部,并且一直在循环运行。我希望如果json文件存在并且文件大小很大,则为0,并且如果选择列表中的一个或多个对象为null,则启用true按钮,然后在单击按钮一次时将按钮设置为false。< / p>
现在,当我删除属于选择列表的层次结构中的对象时,按钮变为启用true,但是当我单击按钮时,他再次变为启用false,而从不启用启用。
private void OnGUI
{
//The saving part:
var selections = Selection.objects.OfType<GameObject>().ToList();
if (selections.Count > 0)
{
for (var i = selections.Count - 1; i >= 0; --i)
{
var selected = selections[i];
transformSelection.Add(selected.transform);
}
TransformSaver.SaveTransform(transformSelection.ToArray());
tempTransformSelection = transformSelection;
transformSelection = new List<Transform>();
}
//The loading part:
var file = @"d:\json\json.txt";
FileInfo fi = new FileInfo(file);
for (int i = 0; i < tempTransformSelection.Count(); i++)
{
if (File.Exists(@"d:\json\json.txt")
&& fi.Length > 0
&& tempTransformSelection[i] == null
&& wasUndo == false)
{
GUI.enabled = true;
wasUndo = false;
}
else
{
GUI.enabled = false;
}
}
if (GUILayout.Button("Undo last"))
{
TransformSaver.LoadTransform();
wasUndo = true;
}
}
答案 0 :(得分:1)
您可以使按钮的绘制取决于wasUndo
。但是实际上,您不需要全局变量。我只是用过showButton
..如果您愿意的话,也可以坚持使用wasUndo
private void OnGUI
{
var file = @"d:\json\json.txt";
FileInfo fi = new FileInfo(file);
// By default asume you don't want to show the button
// it will than only be enabled if the later conditions match
bool showButton = false;
// you can check this already here not in the loop
// if no file -> nothing to do
if(!File.Exists(@"d:\json\json.txt") || fi.Length <= 0) return;
// This is only reached if the file exists and is not empty
// check for null transforms
for (int i = 0; i < tempTransformSelection.Count(); i++)
{
// if not null do nothing
if(tempTransformSelection[i] != null) continue;
// otherwise enable the button and leave the loop
showButton = true;
break;
}
// if not true then button won't be shown
if(!showButton ) return;
if (GUILayout.Button("Undo last"))
{
TransformSaver.LoadTransform();
showButton = false;
}
}
更新
如果您只想禁用按钮而不是使其完全消失,则可以将其包装在EditorGUI.BeginDisabledGroup(bool disabled)
所以
if(!showButton) return;
...
你会做的
EditorGUI.BeginDisabledGroup(!showButton);
if(GUILayout.Button("..."){ //... }
EditorGUI.EndDisabledGroup();