首页 > 解决方案 > 从其他脚本访问创建的精灵时的空引用 [C#]

问题描述

我想从外部文件夹加载动态图像并在运行时设置为精灵,但是有两个脚本可以访问这些图像。当我单击 Button Previous 或 Button Next 时,我从其他脚本调用 sprite 变量,编辑器显示错误空引用。我如何从其他脚本访问,这里是脚本

加载资产H2H.cs

using UnityEngine;
using System.Collections;
using System.IO;

public class LoadAssetH2H : MonoBehaviour {

string[] picPathHome = new string[11];
byte[] picBytesHome;
[HideInInspector]
public Texture2D[] picTex2DHome = new Texture2D[11];
[HideInInspector]
public SpriteRenderer picSRHome;
[HideInInspector]
public Sprite[] picSPHome = new Sprite[11];

string[] picPathAway = new string[11];
byte[] picBytesAway;
[HideInInspector]
public Texture2D[] picTex2DAway = new Texture2D[11];
[HideInInspector]
public SpriteRenderer picSRAway;
[HideInInspector]
public Sprite[] picSPAway = new Sprite[11];

void Awake()
{
    picSRHome = GameObject.Find ("PlayerHomePic").GetComponent<SpriteRenderer> ();
    picSRAway = GameObject.Find ("PlayerAwayPic").GetComponent<SpriteRenderer> ();

    for (int x =0; x<11; x++) 
    {
        picPathHome[x] = Application.dataPath + "/Head To Head/HomeP"+(x+1)+".png";
        if(File.Exists(picPathHome[x]))
        {
            picBytesHome = File.ReadAllBytes(picPathHome[x]);
            picTex2DHome[x] = new Texture2D(256,256);
            picTex2DHome[x].LoadImage(picBytesHome);

            picSPHome [x] = Sprite.Create (picTex2DHome [x], new Rect (0, 0, picTex2DHome [x].width, picTex2DHome [x].height), new Vector2 (0, 0));
        }

        picPathAway[x] = Application.dataPath + "/Head To Head/AwayP"+(x+1)+".png";
        if(File.Exists(picPathHome[x]))
        {
            picBytesAway = File.ReadAllBytes(picPathAway[x]);
            picTex2DAway[x] = new Texture2D(256,256);
            picTex2DAway[x].LoadImage(picBytesAway);

            picSPAway [x] = Sprite.Create (picTex2DAway [x], new Rect (0, 0, picTex2DAway [x].width, picTex2DAway [x].height), new Vector2 (0, 0));
        }
        
    }

    picSRHome.sprite = picSPHome [0];
    picSRAway.sprite = picSPAway [0];
    
}

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
}

按钮.cs

using UnityEngine;
using System.Collections;

public class Button : MonoBehaviour {

LoadAssetH2H loadAssetH2H;

int counterH2H = 0;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnMouseUp()
{
    if (this.gameObject.name == "BtnStanding") 
    {

    }
    if (this.gameObject.name == "BtnHeadToHead") 
    {
        
    }
    if (this.gameObject.name == "BtnStatistic") 
    {
        
    }
    if (this.gameObject.name == "BtnFormation") 
    {
        
    }
    if (this.gameObject.name == "BtnPrev") 
    {
        if(counterH2H>1)
        {
            counterH2H--;

            loadAssetH2H.picSRHome .sprite = loadAssetH2H.picSPHome [counterH2H];
            loadAssetH2H.picSRAway .sprite = loadAssetH2H.picSPAway [counterH2H];
        }
    }
    if (this.gameObject.name == "BtnNext") 
    {
        if(counterH2H<11)
        {
            counterH2H++;   

            loadAssetH2H.picSRHome .sprite = loadAssetH2H.picSPHome [counterH2H];
            loadAssetH2H.picSRAway .sprite = loadAssetH2H.picSPAway [counterH2H];
        }
    }
}
}

我会很感激任何建议。谢谢!

标签: c#unity3dspritetexture2dimage-file

解决方案


推荐阅读