【问题标题】:How can I pass an int to a function that takes in 'ref object'如何将 int 传递给接受“ref object”的函数
【发布时间】: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() 的那个不会更新,因为它只是保存传递给它的值。我想做的是观察变量。

标签: c# unity3d


【解决方案1】:

查看您的代码,您从未分配给引用 value(或 m_Value),因此没有理由将其作为 DebugEntry/AddDebugEntry 中的 ref 参数。

相反,您可以给它一个 Func&lt;object&gt; 委托,在调用它时返回当前值,并在您需要新值时调用它:

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);
}

然后当你打电话给AddDebugEntry:

// add debug item for my x position
myDebugTool.AddDebugEntry( () => transform.position.x,
                           "My X Position",
                           min: -50f,
                           max: 50f);

【讨论】:

    猜你喜欢
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多