简体   繁体   English

我将如何在C#中实现Java代码,反之亦然?

[英]How would I go about implementing java code in c# and vice versa?

I'm attempting to create a game in unity, but Java isn't able to be used in it, so any premade scripts are in C#. 我试图统一创建游戏,但是无法在其中使用Java,因此任何预制脚本都在C#中。 I want to add some stuff in the mechanics of the game, which requires me to alter variables and values in the script, but I only know how to do so in java, so how would I make it so they can effectively communicate? 我想在游戏机制中添加一些内容,这需要我更改脚本中的变量和值,但是我只知道如何在Java中进行更改,因此我将如何使其能够有效地进行交流?

An example from c#: 来自c#的示例:

    protected override void ComputeVelocity()
{
    Vector2 move = Vector2.zero;

    move.x = Input.GetAxis ("Horizontal");
    if (Input.GetButtonDown ("Jump") && grounded) {
        velocity.y = jumpTakeOffSpeed;
    } else if (Input.GetButtonUp ("Jump"))
    {
        if (velocity.y > 0)
            velocity.y = velocity.y * .5f;
    }

    targetVelocity = move * maxSpeed;

}
}

and my java code: 和我的Java代码:

public void keyPressed(KeyEvent e) 
{
    if(e.getKeyCode() == KeyEvent.VK_SHIFT)
    { 
        endTime = (System.currentTimeMillis() / 1000);
        timePassed = endTime - startTime; 
        if(timePassed >= 2)
        {   

            //try to set a time limit or something 

            velocity = overMaxVelocity;
            //set velocity to above usual max for dodgeTime 
            startTime = dodgeTime + (System.currentTimeMillis() / 1000);
        }


    }

}

I'm trying to make it so when shift is pressed, the velocity is changed to a larger value than usual for a small time, but i have no idea where to even begin 我试图做到这一点,所以当按下shift键时,速度会在短时间内更改为比平常大的值,但是我什至不知道从哪里开始

Unity only supports scripts written in C#. Unity仅支持用C#编写的脚本。 It used to also support a version of JavaScript they called UnityScript, but they moved to only officially support C# now. 它曾经还支持一个称为UnityScript的JavaScript版本,但是现在它们仅迁移到正式支持C#。 Fortunately C# is really similar to Java, so you shouldn't have too much trouble translating your scripts to C#. 幸运的是,C#与Java非常相似,因此将脚本转换为C#不会有太多麻烦。 The main challenge would be learning the Unity library. 主要的挑战是学习Unity库。

I wrote some code below that updates an object's speed using Unity library functions. 我在下面编写了一些代码,这些代码使用Unity库函数来更新对象的速度。 Unity has a lot of built-in ways of helping you out as a developer so I'd recommend the tutorials on Unity's website for more on getting started with it. Unity有很多内置的方法可以帮助您成为开发人员,因此我建议您在Unity网站上推荐教程,以获取更多有关使用它的入门信息。

public float speed = 2;
public float speedUpFactor = 2;

// Get the Rigidbody component attached to this gameobject
// The rigidbody component is necessary for any object to use physics)
// This gameobject and any colliding gameobjects will also need collider components
Rigidbody rb;
// Start() gets called the first frame that this object is active (before Update)
public void Start(){
    // save a reference to the rigidbody on this object
    rb = GetComponent<Rigidbody>();
}
}// Update() gets called every frame, so you can check for input here.
public void Update() {

    // Input.GetAxis("...") uses input defined in the "Edit/Project Settings/Input" window in the Unity editor.
    // This will allow you to use the xbox 360 controllers by default, "wasd", and the arrow keys.
    // Input.GetAxis("...") returns a float between -1 and 1
    Vector3 moveForce = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis("Vertical"));
    moveForce *= speed;

    // Input.GetKey() returns true while the specified key is held down
    // Input.GetKeyDown() returns true during the frame the key is pressed down
    // Input.GetKeyUp() returns true during the frame the key is released
    if(Input.GetKey(KeyCode.Shift)) 
    {
        moveForce *= speedUpFactor;
    }
    // apply the moveForce to the object
    rb.AddForce(moveForce);
}

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

相关问题 我将如何实现这个 Java 接口? - How would I go about implementing this Java interface? 从Java运行C#代码,反之亦然 - Run C# code from Java and vice-versa Java 或 C# 十六进制/十进制转换,反之亦然,如何最好? - Java or C# sexagesimal/decimal conversion and vice versa, How is best? 我将如何处理? 不同项目沟通? C# - How would I go about this? Different projects communicating? C# 我将如何跟踪此Java数组代码段 - How would I go about tracing this java arrays code snippet 我将如何在Java 8 Files API上实现与平台无关的文件爬网? - How would I go about implementing platform-agnostic file crawling on the Java 8 Files API? 在Java中,有没有一种方法可以使我从JSONObject转到JSONStringer,反之亦然? - In Java, is there a way I could go from JSONObject to JSONStringer, or vice versa? 我将如何创建输入命令来执行代码的不同部分? -Java - How would I go about creating input commands to execute different part of my code? - Java 我将如何在python中为Java代码编写非常简单的解析器? - How would I go about writing a very simplistic parser for java code in python? 当数据写入协议缓冲区时,如何使用Java触发C ++程序,反之亦然? - How can I have Java trigger C++ programs and vice versa when data is written to protocol buffers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM