【发布时间】:2019-08-01 16:44:11
【问题描述】:
我在 Unity 中使用 IMGUI 来制作一个自定义变量调试器,它允许我向它发送不同原始类型的变量,它会毫不费力地将它们绘制在屏幕上。 (大惊小怪的意思是每次我想检查一个变量在做什么,或者用画布系统连接一些东西时写一个 OnGUI。)我知道显然我可以在检查器中查看这些东西,但是使用这个系统我可以a) 快速添加和删除内容,b) 在运行时进行,无需进行开发构建。
我有一个函数可以接收不同类型的数据并根据其类型对其进行处理。问题是它似乎只有在我按值传递变量时才有效,而不是通过 ref。
因此,例如,如果我按值向它发送一个 int 或一个 float,它工作得很好,但是当我通过 ref 发送它时,我得到这个错误:
错误:CS1503:参数 1:无法从 'ref int' 转换为 'ref 对象'
当AddDebugEntry 被调用时会发生这种情况(整个代码在这个问题的底部):
public void AddDebugEntry(ref object value, string name, DebugDrawStyle style = DebugDrawStyle.Label, float min = 0, float max = 0)
{
DebugEntry newEntry = new DebugEntry(ref value, name, style, min, max);
DebugEntries.Add(newEntry);
}
我相信这可能与拳击有关:Is int (Int32) considered an object in .NET or a primitive (not int?)?
但我不确定如何绕过它:(
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using UnityEngine;
public class DebugEntry
{
public object m_Value;
public string m_name;
public DebugDrawStyle m_Style;
public float m_min;
public float m_max;
public DebugEntry(ref object mValue, string mName, DebugDrawStyle mStyle, float mMin, float mMax)
{
m_Value = mValue;
m_name = mName;
m_Style = mStyle;
m_min = mMin;
m_max = mMax;
}
}
public enum DebugDrawStyle { Label, Slider }
public class DebugTool : MonoBehaviour
{
#region -- Variable Declaration ------------------------------------------------------------------------------------
public Project currentProject;
public Vector2 DebugPosition;
public Vector2 DebugSize;
private int index = 0;
[SerializeField]
private List<DebugEntry> DebugEntries = new List<DebugEntry>();
#endregion -- End Variable Declaration -----------------------------------------------------------------------------
#region -- Initialisation ------------------------------------------------------------------------------------------
// Start is called before the first frame update
void Start()
{
currentProject.DebuggingTool = this;
}
#endregion -- End Initialisation -----------------------------------------------------------------------------------
#region -- Adding / Removing Entries -------------------------------------------------------------------------------
public void AddDebugEntry(ref object value, string name, DebugDrawStyle style = DebugDrawStyle.Label, float min = 0, float max = 0)
{
DebugEntry newEntry = new DebugEntry(ref value, name, style, min, max);
DebugEntries.Add(newEntry);
}
public void RemoveDebugEntry(string name)
{
for (int i = 0; i < DebugEntries.Count; i++)
{
if (DebugEntries[i].m_name.GetHashCode() == name.GetHashCode())
{
DebugEntries.Remove(DebugEntries[i]);
}
}
}
#endregion -- End Adding / Removing Entries ------------------------------------------------------------------------
#region -- GUI Updates ---------------------------------------------------------------------------------------------
private void OnGUI()
{
index = 0;
foreach (var entry in DebugEntries)
{
index++;
switch (Type.GetTypeCode(entry.m_Value.GetType()))
{
case TypeCode.Int16:
switch (entry.m_Style)
{
case DebugDrawStyle.Label:
DrawLabel(entry);
break;
case DebugDrawStyle.Slider:
DrawSlider(entry);
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case TypeCode.Int32:
switch (entry.m_Style)
{
case DebugDrawStyle.Label:
DrawLabel(entry);
break;
case DebugDrawStyle.Slider:
DrawSlider(entry);
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case TypeCode.Int64:
switch (entry.m_Style)
{
case DebugDrawStyle.Label:
DrawLabel(entry);
break;
case DebugDrawStyle.Slider:
DrawSlider(entry);
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case TypeCode.Decimal:
switch (entry.m_Style)
{
case DebugDrawStyle.Label:
DrawLabel(entry);
break;
case DebugDrawStyle.Slider:
DrawSlider(entry);
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case TypeCode.Double:
switch (entry.m_Style)
{
case DebugDrawStyle.Label:
DrawLabel(entry);
break;
case DebugDrawStyle.Slider:
DrawSlider(entry);
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case TypeCode.Single:
switch (entry.m_Style)
{
case DebugDrawStyle.Label:
DrawLabel(entry);
break;
case DebugDrawStyle.Slider:
DrawSlider(entry);
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case TypeCode.Boolean:
DrawLabel(entry);
break;
case TypeCode.String:
DrawLabel(entry);
break;
default:
Debug.Log("Type not found");
throw new ArgumentOutOfRangeException();
}
}
}
#endregion -- End GUI Updates --------------------------------------------------------------------------------------
#region -- GUI Draw Functions --------------------------------------------------------------------------------------
void DrawLabel(DebugEntry entry)
{
GUI.Label(new Rect(DebugPosition.x, DebugPosition.y + (DebugSize.y * index), DebugSize.x, DebugSize.y), entry.m_name.ToString());
index++;
GUI.Label(new Rect(DebugPosition.x, DebugPosition.y + (DebugSize.y * index), DebugSize.x, DebugSize.y), entry.m_Value.ToString());
}
void DrawSlider(DebugEntry entry)
{
GUI.Label(new Rect(DebugPosition.x, DebugPosition.y + (DebugSize.y * index), DebugSize.x, DebugSize.y), entry.m_name.ToString());
index++;
GUI.HorizontalSlider(new Rect(DebugPosition.x, DebugPosition.y + (DebugSize.y * index), DebugSize.x, DebugSize.y),
(float) entry.m_Value, entry.m_min, entry.m_max);
}
#endregion -- End GUI Draw Functions -------------------------------------------------------------------------------
}
【问题讨论】:
-
您实际上是在分配给
AddDebugEntry内的value吗?如果不是,您不需要使用ref。如果你是,你可以创建一个中间变量:object o = myObj; AddDebugEntry(ref o)。这不是由于拳击,只是子类型。 -
想想吧。如果您使用
ref someInteger调用AddDebugEntry,则在该函数内部,将值设置为另一种类型的对象,当函数退出时,您对装箱整数的引用现在是对不同类型对象的引用 -繁荣,坏事发生。如果你检查,你可以传递给你的函数的唯一东西是ref到object类型的变量 -
谢谢。因此,如果我将 int 分配给中间变量,那么我会遇到与按值传递它相同的问题。即当我更新 myObj 时,添加到 AddDebugEntry() 的那个不会更新,因为它只是保存传递给它的值。我想做的是观察变量。