【问题标题】:UnityScript to C# codeUnityScript 到 C# 代码
【发布时间】:2014-07-11 07:28:28
【问题描述】:

我有一个 UnityScript 代码,我想用 C# 转换它,但这就是发生的事情。

Assets/My Scripts/projectileShot2.cs(23,52): error CS0019: Operator * cannot be applied to operands of type 'UnityEngine.Quaternion' and 'float'

Assets/My Scripts/projectileShot2.cs(22,36): error CS0029: Cannot implicitly convert type 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'

Assets/My Scripts/projectileShot2.cs(21,35): error CS0266: Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.Transform'. An explicit conversion exists (are you missing a cast?)

这是 UnityScript 的代码

#pragma strict

function Start () {    
}

var canonPrefab : Transform;
var speed = 0f ;
var angle = 0f;
var time = 5.0f;

function Update(){
if(Input.GetButtonDown("Fire1"))
{
    var canon: Transform =  Instantiate (canonPrefab, transform.position,Quaternion.identity);
    var shootDir = Quaternion.Euler(0, 0, angle) * Vector3.right;
    canon.rigidbody.velocity = shootDir * speed;
    Destroy(canon, 5.0f);
}

if(Input.GetKey (KeyCode.Q)){
        transform.Rotate (new Vector3(0, 0, 30.0f) * Time.deltaTime);
        angle += 1;
    }

if(Input.GetKey (KeyCode.E)){
        transform.Rotate (new Vector3(0, 0, -30.0f) * Time.deltaTime);
        angle -= 1;
    }
}

这是 C# 代码

using UnityEngine;
using System.Collections;

public class projectileShot2 : MonoBehaviour {


    public Transform cannonPrefab;
    public float speed = 0f;
    public float angle = 0f;
    public float time = 0f;


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetButtonDown("Fire1")){
            Transform canon = Instantiate(cannonPrefab, transform.position, Quaternion.identity);
            Quaternion shootDir = Quaternion.Euler(0, 0, angle) * Vector3.right;
            canon.rigidbody.velocity = shootDir * speed;
        }

        if(Input.GetKey (KeyCode.Q)){
            transform.Rotate (new Vector3(0, 0, 30.0f) * Time.deltaTime);
            angle += 1;
        }

        if(Input.GetKey (KeyCode.E)){
            transform.Rotate (new Vector3(0, 0, -30.0f) * Time.deltaTime);
            angle -= 1;
        }
    }
}

我还是这个脚本的新手,还不熟悉数据类型。我希望你能帮助我。谢谢!

【问题讨论】:

    标签: c# unity3d unityscript


    【解决方案1】:

    我在工作中多次遇到这种情况,并且大量使用了ILSpy。您让它编译为程序集(Library/ScriptAssemblies/Assembly-*.dll 文件),然后使用 ILSpy 读取结果并将其反编译为您选择的语言(如 C#)。

    它有一些您仍然需要克服的错误和怪癖,但由于它为您完成了所有繁重的工作,因此您只需手动调整最少的量以适应您的其余代码。

    编辑 1:

    在第 23 行,你有:

    canon.rigidbody.velocity = shootDir * speed;
    

    shootDir 是一个四元数,虽然你可以乘以 Vector3(关于 here 的更多解释),但你不能乘以 speed。试试speed * Vector3.forward 或其他方向。

    编辑 2:

    21行,需要将Instantiate(cannonPrefab,改成Instantiate(cannonPrefab.gameObject,

    编辑 3:

    我不完全确定第 22 行,但我想知道如果将 = 符号的所有内容都包裹在括号中会发生什么,以及这是否会消除该错误/警告。

    【讨论】:

    • 感谢您的回复,我想我已经得到了答案,我所做的是,使用 javascript,我使用 typeof 来识别 shootDir 的数据类型并打印出来。之后我发现它是一个Vector3。然后我在我的 C# 脚本中将数据类型从四元数更改为 Vector3,它已经工作了!谢谢老兄。
    【解决方案2】:

    如果您在使用正确的数据类型时遇到问题,可以随时查看Unity Scripting Reference。我在下面的答案中链接了相应的网站。让我们检查每个错误以及编译器抱怨的原因。

    第 21 行:

    Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.Transform'. An explicit conversion exists (are you missing a cast?)
    

    您正在尝试将 Object 分配给 Transform 变量,虽然编译器无法自行处理它,但它已经提示您什么是正确的解决方案。

    Instantiate 总是返回一个Object 作为结果,所以你必须使用关键字as 将它转换回你的预制件的类型。在这种情况下,您正在克隆一个 Transform 对象,因此正确的代码是:

    Transform canon = Instantiate(cannonPrefab, transform.position, Quaternion.identity) as Transform;
    

    第 22 行:

    Cannot implicitly convert type 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'
    

    现在您尝试将Vector3 分配给Quaternion 变量,但编译器又无法处理此问题。

    如果你 multiplyQuaternionVector3 得到 Vector3,因此你只需将 shootDir 更改为 Vector3

    Vector3 shootDir = Quaternion.Euler(0, 0, angle) * Vector3.right;
    

    第 23 行:

    Operator * cannot be applied to operands of type 'UnityEngine.Quaternion' and 'float'
    

    您不能将Quaternionfloat 相乘,但由于shootDir 现在是Vector3,因此与float 相乘不再是问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多