如何将javascript + jquery转换为unity csharp?

时间:2018-11-16 23:06:24

标签: javascript c# jquery unity3d

var steps_container = $("#steps")[0];

setTimeout(myFunction, 3000);
setInterval(frameUpdate, 50);


var n = 30;
var i=0;
var steps = [];
var steps_x = [];
for (i=0;i<n;i++)
{

    var step = document.createElement("div");

  step.id = "step";
  var step_x = i * 24;
  step.style.left =  step_x.toString() + "px";

  step.style.top = "10px";
  steps[i] = step;
  steps_x[i] = step_x;
  steps_container.appendChild(step);    
}


function frameUpdate()
{
    setStepsPosition();
}

function setStepsPosition()
{
  var i;
  for (i=0;i<n;i++)
  {

    var step_x = steps_x[i];
    step_x = step_x + 1;
    var max_x = (n-1)*24;
    if (step_x >  max_x )
    {
      step_x = 0;
    }
    steps_x[i] = step_x;

    steps[i].style.left = step_x.toString() + "px";

    var r = 5;
    var x_for_atan = step_x*r/max_x - (r/2);
    // extra non linear
    if (step_x<3*24)
    {
        x_for_atan = -(r/2) + 0.5;
    }
    if (step_x>=max_x - 3*24)
    {
        x_for_atan = +(r/2) - 0.5;
    }
    var y = 180 + 120 * Math.atan(x_for_atan);
    steps[i].style.top = y.toString() + "px";
  } 
}

function myFunction() {
  //  alert('Hello');
}


// handle click and add class
button.on("click", function(){
  banner.addClass("alt")
})

来自此jsfiddle:

Javascript jquery

这个想法是一个接一个地一个接一个地移动对象。 统一地,我已经完成了一部分代码,但不确定如何转换和使用此javascript jquery。我的兄弟做了这个javascript jquery示例。

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class MoveObjects : MonoBehaviour
{
    public float speed = 3f;

    private GameObject[] objectstoMove;
    private List<GameObject> objectsMoving = new List<GameObject>();

    // Use this for initialization
    public void Init()
    {
        objectstoMove = GameObject.FindGameObjectsWithTag("Stair");
        objectsMoving = new List<GameObject>(objectstoMove);
    }

    // Update is called once per frame
    void Update()
    {
        if (objectstoMove != null)
        {
            float step = speed * Time.deltaTime;
            for (int i = 0; i < objectstoMove.Length; i++)
            {
                objectsMoving[i].transform.Translate((objectsMoving[i].transform.up + objectsMoving[i].transform.forward) * step);
            }
        }
    }
}

我不知道如何用unity / csharp代码实现javascript / jquery代码。

更新: 我要尝试的是模拟楼梯自动扶梯。 我的第一个脚本创建并定位楼梯:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateStairs : MonoBehaviour
{
    public GameObject stairsPrefab;
    public float delay = 3;
    public int stairsNumber = 5;
    public Vector3 stairsStartPosition;
    public Vector3 stairSize;
    public Vector3 stairsSize;
    public float stepWidthFactor = 1f;
    public MoveObjects moveobjects;

    private Vector3 stairsPosition;
    private GameObject stairsParent;

    // Use this for initialization
    void Start()
    {
        stairsParent = GameObject.Find("Stairs");
        StartCoroutine(BuildStairs());
    }

    // Update is called once per frame
    void Update()
    {

    }

    private IEnumerator BuildStairs()
    {
        for (int i = 1; i <= stairsNumber; i++)
        {

            stairsPosition = new Vector3(
                    stairsStartPosition.x,
                    stairsStartPosition.y + (i * stairsSize.y),
                    stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);

            GameObject stairs = Instantiate(
                    stairsPrefab,
                    stairsPosition,
                    Quaternion.identity);

            stairs.tag = "Stair";
            stairs.transform.parent = stairsParent.transform;
            stairs.transform.localScale = stairSize;

            yield return new WaitForSeconds(delay);
        }

        moveobjects.Init();
    }
}

Stairs

然后,我想使用MoveObjects脚本制作楼梯自动扶梯效果。

1 个答案:

答案 0 :(得分:1)

SetStepsPosition()的内容放入Update()并设置步骤transform.position,而javascript则更改了步骤style.left/top的CSS。

我建议在此处使用Mathf.Repeat处理步骤的x坐标的换行,以避免步骤未对齐。

void Update() {
    float n = (float) objectsMoving.Count;

    foreach (GameObject step in objectsMoving) {
        float step_x = step.transform.position.x;
        float max_x = (n-1f) * 24f; // The javascript has this "24", which is based on the width of the step + the outline.
        step_x = Mathf.Repeat(step_x + speed * Time.time, max_x);

        float r = 5f;
        float x_for_atan = step_x * r / max_x - r/2f;
        if (step_x < 3f * 24f) {
            x_for_atan = -r/2f +.5f;
        }
        if (step_x >= max_x - 3f * 24f) {
            x_for_atan = r/2f - .5f;
        }

        float step_y = 180f + 120f * Math.Atan(x_for_atan);

        step.transform.position = new Vector3(step_x,-step_y,0f); 
        // negative y because the javascript uses top, which starts at 0
        // at the top, and positive goes down. Unity goes the other direction.
    }
}