无法将类型 'Coordinate_data' 隐式转换为 'RandomNumber'

Cannot implicitly convert type 'Coordinate_data' to 'RandomNumber'

我想从 unity 中保存和加载数据,所以我遵循如何从 https://www.youtube.com/watch?v=XOjd_qU2Ido 中保存对象属性但是当我想 return 数据加载已保存的数据时,数据不能隐式获取。

这里是Coordinate_dataClass

using UnityEngine;

[System.Serializable]

public class Coordinate_data
{
    public float[] position;

    public Coordinate_data (RandomNumber coordinate)
    {
        position = new float[3];
        position[0] = coordinate.transform.position.x;
        position[1] = coordinate.transform.position.y;
        position[2] = coordinate.transform.position.z;

    }
}

这里的randomNumber函数是随机显示已经保存的数据或者随机

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RandomNumber : MonoBehaviour
{
    public int MinNum = 0;
    public int MaxNum = 100;

    public Text NilaiRandomX = null;
    public Text NilaiRandomY = null;
    public Text NilaiRandomZ = null;

    public Vector3 position;

    public int StepTime = 2;

    void Start()
    {
        StartCoroutine(RandomNumGenerator());
    }

    IEnumerator RandomNumGenerator()
    {
        while (1 != 0)
        {
            yield return new WaitForSeconds(StepTime);
            int X = UnityEngine.Random.Range(MinNum, MaxNum);
            int Y = UnityEngine.Random.Range(MinNum, MaxNum);
            int Z = UnityEngine.Random.Range(MinNum, MaxNum);

            float nilaix = X;
            float nilaiy = Y;
            float nilaiz = Z;

            position = new Vector3(nilaix, nilaiy, nilaiz);
            transform.position = position;

            //NilaiRandomX.text = + X;
            NilaiRandomX.GetComponent<Text>().text = "" + X;
            //NilaiRandomY.text = Y;
            NilaiRandomY.GetComponent<Text>().text = "" + Y;
            //NilaiRandomZ.text = Z;
            NilaiRandomZ.GetComponent<Text>().text = "" + Z;

        }
    }
}

这里是保存和加载数据的代码

using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public static class SaveSystem
{
    public static void saveCoordinate (RandomNumber coordinate)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "coordinate.bin";
        FileStream stream = new FileStream(path, FileMode.Create);

        Coordinate_data data = new Coordinate_data(coordinate);

        formatter.Serialize(stream, data);

        stream.Close();
    }

    public static RandomNumber LoadPlayer()
    {
        string path = Application.persistentDataPath + "coordinate.bin";
        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);
            Coordinate_data data = formatter.Deserialize(stream) as Coordinate_data;
            stream.Close();

            return data; // fail to return data in here
        }
        else
        {
            Debug.LogError("Save File Not Found in " + path);
            return null;
        }
    }
}

一切正常,除了我现在在 if 语句的 "return person" 上收到此错误消息。

Cannot implicitly convert type 'Coordinate_data' to 'RandomNumber'

有人可以帮忙吗?提前致谢。

因为你已经声明函数 return RandomNumber!

这一行:

public static RandomNumber LoadPlayer() // This function is supposed to return RandomNumber
{
   ...
   Coordinate_data data = formatter.Deserialize(stream) as Coordinate_data;
   return data; // And returning Coordinate_data.
}

看来您是 C# 或 Unity 的新手,所以这里是专业提示:相信编译器所说的。当编译器说 Cannot implicitly convert type 'Coordinate_data' to 'RandomNumber' 时,那么一定有一个错误,表明您在应该使用 RandomNumber.

的地方使用了 Coordinate_data 类型

我不确定所需的操作是什么,但如果您想 return Coordinate_data,只需更改它的 return 类型,例如:

public static Coordinate_data LoadPlayer()

顺便说一句,Implicit type conversion 的意思是,当 某些东西的 类型不匹配时,编译器会尝试用给定的类型来解释它,例如:

private void DoSomthingWithFloat(float x)
{
    ...
}

DoSomethingWithFloat(1);

在上面的例子中,参数x应该是float,但是它被调用时使用了int值。所以编译器 隐式地 将其转换为 float.

但在你的情况下,无法将 Coordinate_data 转换为 RandomNumber,因此编译器抛出了错误。