简体   繁体   English

将js转换为C#

[英]Translating js to C#

Need assistance with translating JS to C#. 需要协助将JS转换为C#。

Original script: 原始脚本:

var target : GameObject;
var fadeDuration : float = 3.0;


function Update(){

    if (target.GetComponent.<Renderer>().material.color.a > 0)
    target.GetComponent.<Renderer>().material.color.a -= Time.deltaTime/fadeDuration;
}

Translated C# script: 翻译的C#脚本:

using UnityEngine;
using System.Collections;

public class FadeOutDeadBody : MonoBehaviour {
GameObject target;
float fadeDuration = 3.0f;
void Update (){

    if (target.GetComponent.<Renderer>().material.color.a > 0)
    target.GetComponent.<Renderer>().material.color.a -= Time.deltaTime/fadeDuration;
}

}

I receive errors from Unity3d that say: 我收到来自Unity3d的错误消息:

folder/FadeOutDeadBody.cs(9,29): error CS1525: Unexpected symbol <', expecting identifier' folder / FadeOutDeadBody.cs(9,29):错误CS1525:意外符号<', expecting标识符'

Any ideas how to fix this problem? 任何想法如何解决此问题?

只需删除GetComponent.<Renderer>的点GetComponent.<Renderer> => GetComponent<Renderer>

Everyone left this out so I decided to post. 每个人都忽略了这个,所以我决定发布。

You cannot modify the alpha of a color directly. 您不能直接修改color的Alpha。 You have to create a new color , modify the alpha, then assign it back to your Material color. 您必须创建一种新color ,修改Alpha,然后将其分配回您的“ Material颜色。 Also remove the dot too. 也要删除点。 This is what it should look like: 它应该是这样的:

public class FadeOutDeadBody : MonoBehaviour
{
    public GameObject target;
    float fadeDuration = 3.0f;

    Renderer renderer;

    void Start()
    {
        renderer = target.GetComponent<Renderer>();
    }

    void Update()
    {

        if (renderer.material.color.a > 0)
        {
            Color color = renderer.material.color;
            color.a -= Time.deltaTime / fadeDuration;
            renderer.material.color = color;
        }
    }
}

GetComponent.<Renderer>() -> GetComponent<Renderer>() You have extra '.' GetComponent.<Renderer>() -> GetComponent<Renderer>()您还有多余的“。”。 in there that is throwing the compiler off. 在那里使编译器无法正常运行。

Edit to respond to your comment on romain-aga's answer 编辑以回应您对romain-aga答案的评论

You need to expand it to look something like this (C# works a bit differently than JS so you can't be as flexible) 您需要对其进行扩展以使其看起来像这样(C#的工作方式与JS有所不同,因此您不能像以前那样灵活)

Renderer r = target.GetComponent<Renderer>();
Color c = r.material.color;
c.a -= Time.deltaTime/fadeDuration;
r.material.color = c;

The r, g, b, and a properties of a color in C# are read-only, so you have to modify the entire color value. C#中的r,g,b和颜色属性是只读的,因此您必须修改整个颜色值。

Note: I don't have Unity on the computer I'm at right now, so this is untested. 注意:我现在所在的计算机上没有Unity,因此未经测试。 We'll see how well my memory is working today. 我们将看到我今天的记忆运行状况如何。

Instead of this 代替这个

target.GetComponent.<Renderer>()

you probably need this 你可能需要这个

target.GetComponentInParent<Renderer>()

or this 或这个

target.GetComponent(typeof(Renderer))

Just be careful when "translating" code from one language to another, as conventions are language specific. 当将代码从一种语言“翻译”到另一种语言时,请务必小心,因为约定是特定于语言的。 Looking at the unity docs, the proper way to call GetComponent is as a generic, so you can fix this particular issue by simply removing the period in your code calling GetComponent. 查看统一文档,调用GetComponent的正确方法是通用的,因此您可以通过简单地删除代码中调用GetComponent的句点来解决此特定问题。

In general, you will probably have more problems like this due to C# being very different from JavaScript, so just be aware of this. 通常,由于C#与JavaScript截然不同,您可能会遇到更多类似这样的问题,所以请注意这一点。

Reference: GetComponent documentation 参考: GetComponent文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM