C# 如何将int传递给接受';参考对象';
我正在Unity中使用IMGUI来制作一个自定义变量调试器,它允许我向它发送不同基本类型的变量,它将在屏幕上绘制它们,而不需要太多麻烦。(我所说的“大惊小怪”是指每次我想检查一个变量在做什么,或者将某个东西连接到画布系统时,都要编写一个OnGUI。)我知道,很明显,我可以在inspector中查看这些东西,但有了这个系统,我可以a)快速添加和删除东西,b)在运行时不必进行开发构建 我有一个函数,它接收不同类型的数据,并根据其类型处理数据。问题是,只有通过值传递变量,而不是通过ref传递变量时,它似乎才起作用 例如,如果我通过值发送int或float,它工作得很好,但是当我通过ref发送它时,我会得到以下错误: 错误:CS1503:参数1:无法从'ref int'转换为'ref 对象的 调用C# 如何将int传递给接受';参考对象';,c#,unity3d,C#,Unity3d,我正在Unity中使用IMGUI来制作一个自定义变量调试器,它允许我向它发送不同基本类型的变量,它将在屏幕上绘制它们,而不需要太多麻烦。(我所说的“大惊小怪”是指每次我想检查一个变量在做什么,或者将某个东西连接到画布系统时,都要编写一个OnGUI。)我知道,很明显,我可以在inspector中查看这些东西,但有了这个系统,我可以a)快速添加和删除东西,b)在运行时不必进行开发构建 我有一个函数,它接收不同类型的数据,并根据其类型处理数据。问题是,只有通过值传递变量,而不是通过ref传递变量时,它
AddDebugEntry
时会发生这种情况(整个代码位于问题的底部):
我相信这可能与拳击有关:
但我不知道如何避开它:(
使用系统;
使用系统集合;
使用System.Collections.Generic;
运用系统反思;
使用System.Reflection.Emit;
使用UnityEngine;
公共类调试条目
{
公共对象m_值;
公共字符串m_name;
公共建筑风格m_风格;
公众花车(mu min),;
公共浮动m_max;
public DebugEntry(ref object mValue、string mName、DebugDrawStyle mStyle、float mMin、float mMax)
{
m_值=m值;
m_name=mName;
m_风格=mStyle;
m_min=mMin;
m_max=mMax;
}
}
公共枚举DebugDrawStyle{标签,滑块}
公共类调试工具:MonoBehavior
{
#区域——变量声明------------------------------------------------------------------------------------
公共工程项目;
公共向量2位置;
公共向量2的大小;
私有整数指数=0;
[序列化字段]
private List DebugEntries=new List();
#endregion—结束变量声明-----------------------------------------------------------------------------
#地区-初始化------------------------------------------------------------------------------------------
//在第一帧更新之前调用Start
void Start()
{
currentProject.DebuggingTool=此;
}
#endregion—结束初始化-----------------------------------------------------------------------------------
#区域--添加/删除条目-------------------------------------------------------------------------------
public void AddDebugEntry(引用对象值、字符串名称、DebugDrawStyle样式=DebugDrawStyle.Label、float min=0、float max=0)
{
DebugEntry newEntry=新的DebugEntry(参考值、名称、样式、最小值、最大值);
DebugEntries.Add(newEntry);
}
public void RemoveDebugEntry(字符串名称)
{
for(int i=0;ipublic 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);
}
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 -------------------------------------------------------------------------------
}
public class DebugEntry
{
public Func<object> m_getValue;
public Type m_Type;
...
public DebugEntry(Func<object> getValue, string mName, DebugDrawStyle mStyle, float mMin, float mMax)
{
m_getValue = getValue;
m_Type = getValue().GetType();
m_name = mName;
m_Style = mStyle;
m_min = mMin;
m_max = mMax;
}
...
public void AddDebugEntry(Func<object> getValue, string name, DebugDrawStyle style = DebugDrawStyle.Label, float min = 0, float max = 0)
{
DebugEntry newEntry = new DebugEntry(getValue, name, style, min, max);
DebugEntries.Add(newEntry);
}
...
switch (Type.GetTypeCode(entry.m_Type))
{
...
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_getValue().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_getValue(), entry.m_min, entry.m_max);
}
// add debug item for my x position
myDebugTool.AddDebugEntry( () => transform.position.x,
"My X Position",
min: -50f,
max: 50f);