【问题标题】:Having Problems with Unity TemplateUnity 模板有问题
【发布时间】:2020-06-20 20:09:32
【问题描述】:

谁能解释一下错误是什么?

IEnumerator GetPictureCor(string url, FriendData friend)


    Sprite sprite = new Sprite();
    WWW www = new WWW(url);
    yield return www;
    sprite = Sprite.Create(www.texture, new Rect(0, 0, 128, 128), new Vector2(0, 0), 1f);
    friend.picture = sprite;
    //      print ("get picture for " + url);
}

错误是:

Assets\ADogsAdventure\Scripts\Integrations\FacebookManager.cs(419,29):错误 CS1729:“Sprite”不包含采用 0 个参数的构造函数

【问题讨论】:

标签: unity3d


【解决方案1】:

由于错误状态,Sprite 没有采用 0 参数的构造函数,这是因为 Sprite 不打算与 Sprite sprite = new Sprite() 一起使用,而是有一个名为 Sprite.Create(); 的方法应该用于创建一个精灵。

作为per the Unity docsSprite.Create()

Sprite.Create 创建一个可以在游戏应用程序中使用的新 Sprite。需要加载纹理并将其分配给 Create 以控制新 Sprite 的外观。

public class spriteCreate : MonoBehaviour
{
    public Texture2D tex;
    private Sprite mySprite;
    private SpriteRenderer sr;

    void Awake()
    {
        sr = gameObject.AddComponent<SpriteRenderer>() as SpriteRenderer;
        sr.color = new Color(0.9f, 0.9f, 0.9f, 1.0f);

        transform.position = new Vector3(1.5f, 1.5f, 0.0f);
    }

    void Start()
    {
        mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 100, 30), "Add sprite"))
        {
            sr.sprite = mySprite;
        }
    }
}

有关错误 CS1729 的更多信息可以在 C# documentation 中找到,它解释了给构造函数没有实现您传递的参数数量(在本例中为 0,或任何其他数量的参数,因为有不是Sprite 的公共构造函数)

来自上面链接的微软文档:

当您直接或间接调用类的构造函数但编译器找不到任何具有相同数量参数的构造函数时会发生此错误。在下面的示例中,测试类没有带任何参数的构造函数。因此它只有一个无参数的构造函数,它接受零个参数。因为在生成错误的第二行中,派生类没有声明自己的构造函数,所以编译器提供了无参数构造函数。该构造函数调用基类中的无参数构造函数。因为基类没有这样的构造函数,所以生成了CS1729。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 2010-09-28
    • 2017-02-14
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多