首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能隐式地将“Coordinate_data”转换为“RandomNumber”

不能隐式地将“Coordinate_data”转换为“RandomNumber”
EN

Stack Overflow用户
提问于 2020-04-16 02:08:34
回答 1查看 37关注 0票数 0

我希望从统一保存和加载数据,所以我遵循如何从https://www.youtube.com/watch?v=XOjd_qU2Ido保存对象属性,但是当我想返回数据来加载保存的数据时,数据不能隐式地获得。

这里是Coordinate_data类

代码语言:javascript
复制
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函数是随机的,显示已经保存或随机保存的数据。

代码语言:javascript
复制
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;

        }
    }
}

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

代码语言:javascript
复制
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语句中的“返回人”上得到这个错误消息外,一切都很好。

不能隐式地将类型“Coordinate_data”转换为“RandomNumber”

有人能帮忙吗?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-16 03:24:33

因为您已经声明函数来返回RandomNumber

在这一行:

代码语言:javascript
复制
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'时,一定有一个错误,您使用的是Coordinate_data类型,应该使用RandomNumber

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

代码语言:javascript
复制
public static Coordinate_data LoadPlayer()

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

代码语言:javascript
复制
private void DoSomthingWithFloat(float x)
{
    ...
}

DoSomethingWithFloat(1);

在上面的例子中,参数x应该是float,但是它是用int值调用的。因此编译器隐式地将其转换为float

但在您的示例中,无法将Coordinate_data转换为RandomNumber,因此编译器抛出了错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61241568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档