- 1、unity3d是圓柱體以一定的速度轉過一定的角度js代碼
- 2、使用Unity3d無法正常編寫JS腳本
- 3、在unity3d裡面用C#連接資料庫得到數據,能不能傳到JS裡面使用啊?怎麼在JS裡面得到那些數據啊?
- 4、如何調用unity3d webgl 的js
- 5、unity3D里把c#腳本改寫成js腳本,如下程序如何改寫成js腳本,寫了半天都感覺有些問題
- 6、unity3d js 和web js的區別
#pragma strict
public var capsule:GameObject;
private var i:float = 0;
public var speed:float;
public var allTime:float;
function Start () {
}
function Update () {
i+=Time.deltaTime;
if(iallTime){
capsule.transform.Rotate(Vector3.up*Time.deltaTime*speed);
}
}
解釋一下,capsule這個不用說就是你的圓柱體了,inspector面板中賦值一下就好了。i是一個計時用的,不用管。speed,就是旋轉的速度了,speed假如等於60,就是每秒旋轉60度,allTime就是旋轉的總共的時間。例如,你想這個圓柱以每秒60度旋轉120度就停止,這是speed =60,allTime = 2,就好了。不過這樣的弊端就是它旋轉的不是恰好120,可能119.8度這樣的,有誤差
你的編輯器選錯了。 在edit-preferences中的external tools -external script editor 框的下拉菜單選unisciTE就可以,如果沒有這個選項那麼選browse,定位到Editor\Data\Tools\UniSciTE\UniSciTE.exe.
首先 如果你是直接訪問資料庫的,我建議你不要這麼干,可以通過webservice 或者其他通信方式間接訪問資料庫,因為你這麼做看似非常快但是很不安全。
方法01
————————JS訪問C#的變數—————————–
JS腳本:
import System.Reflection;
var ee = 0;
var ddf:GameObject;
function OnGUI ()
{
var targetController1:Component= ddf.GetComponent(“ctrl”);
var fieldInfo: FieldInfo=targetController1.GetType().GetField(“csvalue”);
if(GUI.Button(Rect(100,100,100,100),”Call”))
{
ee=fieldInfo.GetValue(targetController1);
print(ee.ToString());
}
}
C#腳本:
using UnityEngine;
using System.Collections;
public class ctrl : MonoBehaviour
{
public int csvalue = 100;
void Start ()
{
}
void Update ()
{
}
}
———————–c#訪問JS變數————————
c#代碼:
using UnityEngine;
using System.Collections;
using System;
using System.Reflection;
public class CtoJS : MonoBehaviour {
public GameObject tt;
public String moneycount;
public String boncount;
// Use this for initialization
void Start ()
{
Component AComponent = tt.GetComponent(“CllisionsSomeThing”);
FieldInfo fieldInfo = AComponent.GetType().GetField(“MC”);
FieldInfo fieldInfo1 = AComponent.GetType().GetField(“BomCount”);
moneycount = fieldInfo.GetValue(AComponent).ToString();
boncount= fieldInfo1.GetValue(AComponent).ToString();
print(moneycount);
print(boncount);
}
void Update ()
{
}
}
js代碼:
#pragma strict
var MC = “sdfsdfs”;
var BomCount = 1000;
function Start () {
}
function Update () {
}
————————————————————————————————-
方法02
兩個文件 test1.js 和 test2.cs
test1.js
function OnGUI()
{
if(GUI.Button(Rect(25,25,100,30),”JS Call CS” ))
{
var c = gameObject.GetComponent(“test2”);
c.PrintTest();
}
}
function testPrint()
{
print(“CS Call JS”);
}
test2.cs
using UnityEngine;
using System.Collections;
public class test2: MonoBehaviour {
void OnGUI()
{
if(GUI.Button(new Rect(25,70,100,30), “CS Call JS”))
{
test1 c = (test1)gameObject.GetComponent(“test1”);
c.testPrint();
}
}
void PrintTest()
{
print(“JS Call CS”);
}
}
這裡必須要注意的是JS文件必須是在 “Standard Assets”、 “Pro Standard Assets” 和 “Plugins” 這三個目錄中的任何一個里,而CS文件不能與JS文件在一個目錄中。原因是,這三個目錄里的腳本被最先編譯,”Editor”目錄里的稍後編譯,其他的腳本最後編譯。目前Unity3D的2.5的版本似乎不支持C# 3.0,所以無法用var的關鍵字,這樣就只支持強類型,所以如果在一個目錄下則CS文件無法讀取JS里的方法,也就無法編譯通過了。而JS調用CS方法則無此限制。
開發網頁js代碼交互:
方向一:你可以調用Application.ExternalCall() 和Application.ExternalEval()在你嵌入的網頁中執行 JavaScript代碼.
方向二:在網頁的js代碼中執行Unity中GameObjects的方法:例如
SendMessage (‘MyGameObject’, ‘MyFunction’, ‘foobar’)
《二》Application.ExternalCall調用JS函數
public static function ExternalCall(functionName: string, params args: object[]): void;
public var material : Material;
function OnPostRender(){
if(!material){
Debug.LogError(“ss”);
return;
}
material.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES);
DrawLine(0,0,200,100);
DrawLine(0,50,200,150);
DrawLine(0,100,200,200);
GL.End();
}
function DrawLine(var x1:float,var y1:float,var x2:float,var y2:float){
GL.Vertex(new Vector3(x1/Screen.width, y1/Screen.height, 0));
GL.Vertex(new Vector3(x2/Screen.width, y2/Screen.height, 0));
}
unity中的js與其說是js,不如說是Unity Script。unity中的js是會經過編譯的,其性能和本地速度差不多。在官方教材《unity 4.x從入門到精通》中unity稱C#,Boo,JS的性能是差不多的。
unity中的js腳本是可以和C#腳本等值替換的,所以自然也有一大堆數據類型,對象繼承等傳統語言及OOP的概念。不過var speed = 5這麼寫也是可以的,因為編譯器會自動理解成var speed:int = 5。但是其他數據類型比如GameObject、Transform就不行了,必須在聲明變數時指定數據類型。
自然,很多標準js中的特性在unity中也不支持,比如高階函數,閉包等。
Unity3D中的Math對象叫做Mathf。
Unity中的js可以直接調用Mono,C#封裝的dll等。
Unity中的調試語句用Debug.Log。
每行後面必須有分號。
總之,Unity中的js是會在運行前被編譯成本地代碼的。和標準js僅是寫法比較相似,內在是完全不同的。比如js是非阻塞的,而unity中的js是阻塞的;js是動態語言,而unity中的js則是不折不扣的靜態語言。所以前端攻城獅們想要熟練掌握untiy的js的話最好的方法就是多參考官方的js腳本,相信上手還是很快的,畢竟語法很相似。
原創文章,作者:QTQ0M,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126682.html