1.右键菜单
function OnGUI(){
if(Input.GetMouseButton(1))
{
GUILayout.BeginArea (new Rect (Input.mousePosition.x,Screen.height-Input.mousePosition.y,200,200));
GUILayout.Box("This is a Context Menu");
GUILayout.EndArea ();
}
}
/*
if(Input.GetMouseButton(0))
Debug.Log("Pressed left click.");
if(Input.GetMouseButton(1))
Debug.Log("Pressed right click.");
if(Input.GetMouseButton(2))
Debug.Log("Pressed middle click.");
*/
2.用辅助键/双键控制视角
using UnityEngine;
using System.Collections;
public class ControlCamerMove : MonoBehaviour {
void Update () {
if(Input.GetKeyDown(KeyCode.LeftControl)){
this.GetComponent<MouseLook>().enabled = false;
}
if(Input.GetKeyUp(KeyCode.LeftControl)){
this.GetComponent<MouseLook>().enabled = true;
}
}
}
3.与网页交互-打开网页
function OnGUI()
{
GUILayout.Label("当前场景:"+ Application.loadedLevelName);
if(GUILayout.Button("打开网页"))
Application.OpenURL("www.baidu.com");
}
4.在unity中播放视频--绘制
#pragma strict
var movTexture:MovieTexture;
function Start ()
{
movTexture.loop = true;
}
function OnGUI()
{
GUI.DrawTexture(new Rect (0,0,Screen.width,Screen.height),movTexture,ScaleMode.StretchToFill);
if(GUILayout.Button("播放/继续"))
{
if (!movTexture.isPlaying)
{
movTexture.Play();
}
}
if(GUILayout.Button("暂停播放"))
{
movTexture.Pause();
}
if(GUILayout.Button("停止播放"))
{ 贴图
movTexture.Stop();
}
}
5.在unity中播放视频--作为plane的材
#pragma strict
var movTexture:MovieTexture;
function Start ()
{
this.renderer.material.mainTexture = movTexture;
movTexture.loop = true;
}
function OnGUI()
{
if(GUILayout.Button("播放/继续"))
{
if (!movTexture.isPlaying)
{
movTexture.Play();
}
}
if(GUILayout.Button("暂停播放"))
{
movTexture.Pause();
}
if(GUILayout.Button("停止播放"))
{
movTexture.Stop();
}
}
6.用材质偏移模拟水流效果
using UnityEngine;
using System.Collections;
public class Whatflow : MonoBehaviour {
public float scrollSpeed=3;
void Update () {
float offset = Time.time*scrollSpeed;
renderer.material.SetTextureOffset("_MainTex",new Vector2(0,offset));
}
}
7.从网页上获取一张图片并动态使用
var url = "http://img.sucai.redocn.com/attachments/images/201011/20101110/20101109_da85542f8635f3c5b963vil1B8qeeS9O.jpg";
function Start () {
var www : WWW = new WWW (url);
//定义www为WWW类型并且等于被下载的内容。
yield www;
//等待www完全下载。
this.renderer.material.mainTexture = www.texture;
//将下载下来的WWW中的图片赋予到默认物体的材质上进行渲染出来
}
8.点击物体使其变为半透明
//要将物体的shader方式改为Tansparent/Diffuse,才可以实现点击时变为半透明
function Start()
{
this.renderer.material.shader = Shader.Find("Transparent/Diffuse");
}
function OnMouseDown () {
this.renderer.material.color.a=0.5;
}
function OnMouseUp() {
if(this.renderer.material.color.a!=1)
{
this.renderer.material.color.a=1;
}
}
9.控制物体移动(3种)
#pragma strict
var movespeed = 20;
//1.键盘控制,按W/A/S/D可以朝着不同方向移动
function Update () {
if(Input.GetKey(KeyCode.W))
{
Debug.Log("W向前移动");
this.transform .Translate(Vector3.forward*Time.deltaTime*movespeed);
}
if(Input.GetKey(KeyCode.S))
{
Debug.Log("S向后移动");
this.transform .Translate(-Vector3.forward*Time.deltaTime*movespeed);
}
if(Input.GetKey(KeyCode.A))
{
Debug.Log("A向左移动");
this.transform .Translate(Vector3.left*Time.deltaTime*movespeed);
}
if (Input.GetKey(KeyCode.D))
{
Debug.Log("D向右移动");
this.transform .Translate(Vector3.right*Time.deltaTime*movespeed);
}
if(Input.GetKey(KeyCode.UpArrow))
{
Debug.Log("向前移动");
this.transform .Translate(Vector3.forward*Time.deltaTime*movespeed);
}
if(Input.GetKey(KeyCode.DownArrow))
{
Debug.Log("向后移动");
this.transform .Translate(-Vector3.forward*Time.deltaTime*movespeed);
}
if(Input.GetKey(KeyCode.LeftArrow))
{
Debug.Log("向左移动");
this.transform .Translate(Vector3.left*Time.deltaTime*movespeed);
}
if(Input.GetKey(KeyCode.RightArrow))
{
Debug.Log("向右移动");
this.transform .Translate(Vector3.right*Time.deltaTime*movespeed);
}
}
//2.在物体上按下鼠标,物体朝着某给定方向移动
function OnMouseUp() {
this.transform .Translate(Vector3.left * Time.deltaTime * movespeed);
}
//3.添加按钮,通过按钮实现移动
function OnGUI()
{
if(GUILayout.Button("向前移动", GUILayout.Height(50)))
this.transform.Translate(Vector3.forward*Time.deltaTime*movespeed);
if(GUILayout.Button("向后移动", GUILayout.Height(50)))
this.transform.Translate(-Vector3.forward*Time.deltaTime*movespeed);
if(GUILayout.Button("向左移动", GUILayout.Height(50)))
this.transform.Translate(Vector3.left*Time.deltaTime*movespeed);
if(GUILayout.Button("向右移动", GUILayout.Height(50)))
this.transform.Translate(Vector3.right*Time.deltaTime*movespeed);
GUILayout.Label("立方体的位置:"+this.transform .position);
}
10.音频控制
using UnityEngine;
using System.Collections;
public class sound : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI() {
// 这个函数会自动触发,在游戏界面上启动按钮之类的用户界面。
// 下面的代码就顾名思义的吧。自己尝试一下就知道了。
if (GUI.Button(new Rect(0, 60, 100, 50), "Play"))
audio.Play();
if (GUI.Button(new Rect(0, 120, 100, 50), "Pause"))
audio.Pause();
if (GUI.Button(new Rect(0, 180, 100, 50), "Stop"))
audio.Stop();
}
}
11.自定义控制键
#pragma strict
var my_key:KeyCode;
function Start () {
my_key = KeyCode.T;
}
function Update () {
if (Input.GetKeyDown(my_key))
{
this.transform.position.y +=2;
}
}