unity3d 21

来源:互联网 发布:2016淘宝客导购源码 编辑:程序博客网 时间:2024/05/17 15:57

LocalScale_test.cs

using UnityEngine;using System.Collections;public class LocalScale_test : MonoBehaviour {GameObject obj;float scaleZ = 1.0f;float scaleX = 1.0f;float scaleY = 1.0f;// Use this for initializationvoid Start () {obj = GameObject.Find ("Cube");}// Update is called once per framevoid Update () {}void OnGUI(){GUILayout.Label("x轴缩放");scaleX = GUILayout.HorizontalSlider(scaleX,1.0f,50.0f,GUILayout.Width(100));GUILayout.Label("y轴缩放");scaleY = GUILayout.HorizontalSlider(scaleY,1.0f,50.0f,GUILayout.Width(100));GUILayout.Label("z轴缩放");scaleZ = GUILayout.HorizontalSlider(scaleZ,1.0f,50.0f,GUILayout.Width(100));obj.transform.localScale =new  Vector3(scaleX,scaleY,scaleZ);}}

/*
首先给大家介绍一个名词  #pragma strict
#pragma strict
严谨编译模式
性能优化:JS中强制使用静态类型,脚本顶部添加#pragma strict。然后,unity将在脚本中禁用动态类型,强制使用静态类型,如果一个类型未知。Unity将报告编译错误。
可能会报这样的一个错误:(方法名)  is not a member of 'UnityEngine.Component'.  解决这个错误的方法就是将 #pragma strict 删掉就好了。


其中JS的文件必须在  这三个文件中的一个"Standard Assets"、 "Pro Standard Assets" 和 "Plugins"  没有的话自己创建就好。
*/


Cs_test.cs

using UnityEngine;using System.Collections;public class Cs_test : MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}void OnGUI(){if (GUI.Button (new Rect(100,50,200,100),"C#调用JavaScript")) {Js_test jsScript = (Js_test)GetComponent("Js_test");jsScript.CallMe("我来自Java");}} void CallMe(string test){Debug.Log (test);}}
Js_test.js

//#pragma strict/*首先给大家介绍一个名词  #pragma strict#pragma strict严谨编译模式性能优化:JS中强制使用静态类型,脚本顶部添加#pragma strict。然后,unity将在脚本中禁用动态类型,强制使用静态类型,如果一个类型未知。Unity将报告编译错误。可能会报这样的一个错误:(方法名)  is not a member of 'UnityEngine.Component'.  解决这个错误的方法就是将 #pragma strict 删掉就好了。其中JS的文件必须在  这三个文件中的一个"Standard Assets"、 "Pro Standard Assets" 和 "Plugins"  没有的话自己创建就好。*/function Start () {}function Update () {}function OnGUI(){if(GUILayout.Button("JavaScript调用C#")){var cs = this.GetComponent("Cs_test");cs.CallMe("哦来自C#");}}function CallMe(test : String){Debug.Log(test);}

js only can use  GameObject.Getcomponent(string)

but both of Getcomponent<>() and Getcomponent can use in cs.



0 0
原创粉丝点击