【问题标题】:Accesing a variable inside a class, inside another class, inside a namespace访问一个类内、另一个类内、命名空间内的变量
【发布时间】:2021-01-01 21:44:05
【问题描述】:

我是一名新手,从代码世界开始,我是一名视频游戏开发人员,实际上是第一次实习。

我的问题是这个,我在一个类、另一个类、一个命名空间中有一个变量,我需要从不同的脚本访问该变量。

using UnityEngine;
using UnityEngine.Events;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace PaintIn3D
{
    
    [RequireComponent(typeof(Renderer))]
    [RequireComponent(typeof(P3dPaintable))]
    [HelpURL(P3dHelper.HelpUrlPrefix + "P3dPaintableTexture")]
    [AddComponentMenu(P3dHelper.ComponentMenuPrefix + "Paintable Texture")]
    public class P3dPaintableTexture : P3dLinkedBehaviour<P3dPaintableTexture>
    {
        public enum StateType
        {
            None,
            FullTextureCopy,
            LocalCommandCopy
        }

        public enum MipType
        {
            Auto,
            ForceOn,
            ForceOff
        }

        [System.Serializable] public class PaintableTextureEvent : UnityEvent<P3dPaintableTexture> {}

        
        public P3dSlot Slot { set { slot = value; } get { return slot; } } [SerializeField] private P3dSlot slot = new P3dSlot(0, "_MainTex");

        
        public P3dChannel Channel { set { channel = value; } get { return channel; } } [SerializeField] private P3dChannel channel;

        
        public P3dGroup Group { set { group = value; } get { return group; } } [SerializeField] private P3dGroup group;

        
        public StateType State { set { state = value; } get { return state; } } [SerializeField] private StateType state;

        
        public int StateLimit { set { stateLimit = value; } get { return stateLimit; } } [SerializeField] private int stateLimit;

        
        public string SaveName { set { saveName = value; } get { return saveName; } } [SerializeField] private string saveName;

        
        public string ShaderKeyword { set { shaderKeyword = value; } get { return shaderKeyword; } } [SerializeField] private string shaderKeyword;

    
        public RenderTextureFormat Format { set { format = value; } get { return format; } } [SerializeField] private RenderTextureFormat format;

    
        public MipType MipMaps { set { mipMaps = value; } get { return mipMaps; } } [SerializeField] private MipType mipMaps;

    
        public int Width { set { width = value; } get { return width; } } [SerializeField] private int width = 512;

    
        public int Height { set { height = value; } get { return height; } } [SerializeField] private int height = 512;

    
        public Color Color { set { color = value; } get { return color; } } [SerializeField] private Color color = Color.white;

    
        public Texture Texture { set { texture = value; } get { return texture; } } [SerializeField] private Texture texture;


        public event System.Action<P3dCommand> OnAddCommand;

    
        public event System.Action<bool> OnModified;
    }
}

第二堂课的内容更多,但我认为不相关(

我需要访问 Texture 变量,以便应用程序的用户可以通过文件资源管理器插入 Texture,但我不知道该怎么做。

我试过这个:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using SmartDLL;
using System.IO;
using PaintIn3D;


public class Explorer2 : MonoBehaviour
{
    public P3dPaintableTexture newTexture;
    /*public GameObject eImage;*/
    public Button openExplorerButton;
    private bool readImage = false;

    public SmartFileExplorer fileExplorer = new SmartFileExplorer();

    void OnEnable()
    {
        openExplorerButton.onClick.AddListener(delegate { ShowExplorer(); });
    }

    void Update()
    {
        if (fileExplorer.resultOK && readImage)
        {
            OpenImage(fileExplorer.fileName);
            readImage = false;
        }
    }

    void ShowExplorer()
    {
        string initialDir = @"C:\";
        bool restoreDir = true;
        string title = "Open a Image File";
        string defExt = "png";
        string filter = "txt files (*.png)|*.png";
        fileExplorer.OpenExplorer(initialDir, restoreDir, title, defExt, filter);
        readImage = true;
    }



    void OpenImage(string path)
    {
        WWW www = new WWW("file:///" + path);
        newTexture.Texture = www.texture;     /* <----- This Line */
        
    }
}

但这向我展示了这个结果:

检查员

在这张图片的左边,是当我直接将它拖过检查器时纹理的显示方式,右边是当我使用文件资源管理器上的信号代码行访问变量时它的显示方式,它的就像它通过了一半但纹理图像不完全在变量中,并且纹理没有应用于对象。

【问题讨论】:

  • 您需要等待下载完成才能更新它。使用协程或 unitywebrequest。

标签: c# class unity3d variables namespaces


【解决方案1】:

下载的纹理似乎没有名称,但引用正确,否则字段会显示None(Texture)

你可以设置一个,例如

newTexture.name = "Oh now I have a name";

但是,您不会等待直到下载完成,因此您会尝试在完全下载之前使用纹理。

我在你的代码中没有看到这个纹理应该如何应用于其他任何东西。


一般注意WWW 被长期标记为废弃,您应该使用UnityWebRequestTexture.GetTexture

void OpenImage(string path)
{
    StartCoroutine(OpenImageRoutine(path));
}

private IEnumerator OpenImageRoutine(string path)
{
    using (var uwr = UnityWebRequestTexture.GetTexture("file:///" + path))
    {
        yield return uwr.SendWebRequest();

        if (uwr.isNetworkError || uwr.isHttpError)
        {
            Debug.Log(uwr.error);
        }
        else
        {
            // Get downloaded texture
            newTexture.Texture = DownloadHandlerTexture.GetContent(uwr);
        }
    }
}

【讨论】:

  • 带有变量的脚本比我发布的要大,我认为这对每个人来说可能都很烦人,所以我省略了所有不是变量的东西,但它是一个四边形上的脚本,在游戏,将纹理应用于四边形并使其可修改(可绘制)。无名纹理的问题在于,它不适用于四边形,其他纹理不会以相同的方式加载到同一个应用程序上,但其变量只是在一个类下,所以我没有问题访问它们.
  • 关于带有 www 的提示,感谢您的建议,我知道它已经过时了,但没有人教我如何替换它。
  • 我不明白等待纹理完全下载的部分,我正在使用我的硬盘上的图像。
  • 您仍然必须等到它们完全加载... FileIO 需要时间。请参考我提供的链接中的示例,它基本上显示了如何等待请求的结果......WWW 类基本相同:您必须在 Coroutine 中使用yield return,直到加载完成完成,您可以实际使用纹理
猜你喜欢
  • 1970-01-01
  • 2012-08-06
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 2012-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多