【发布时间】: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