利用射线控制多个按钮来打开网页
点击上面按钮打开各个网页
1.首先利用NGUI建立四个按钮 如图
2.再建立一个空物体用于挂下面的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dakaiwangye : MonoBehaviour {
public GameObject zhuce; //首先定义4个物体代表四个按钮
public GameObject zhinan;
public GameObject luntan;
public GameObject kefu;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray myRay = Camera.main.ScreenPointToRay(Input.mousePosition);//建立射线 起点为摄像机 终点为鼠标的位置
RaycastHit hit;
if(Input.GetMouseButtonDown(0)) //鼠标左键点击
{
if(Physics.Raycast(myRay,out hit)) //发射射线(起点终点,返回信息)
{
GameObject go = hit.collider.gameObject; //实例化一个物体go 再将射线碰撞的物体赋给go
switch(go.name)
{
case "zhuce":
Application.OpenURL("www.qq.com"); //如果射线碰撞的物体名是"zhuce" 就打开网页
break;
case "zhinan":
Application.OpenURL("www.qq.com");
break;
case "luntan":
Application.OpenURL("www.qq.com");
break;
case "kefu":
Application.OpenURL("www.qq.com");
break;
}
}
}
}
}
利用射线进行UISprite贴图的替换(LOL选择英雄)
上述图片中的英雄皆为按钮, 用按钮中的图片替换左侧EZ的贴图
1.首先利用UGUI建立多个按钮,缩放好大小
2.建立一个UISprite 底板用作黑色来遮盖头像 也用作最初的替换
3.开始写以下脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class xuankuang : MonoBehaviour {
public UISprite obj; //public一个UISprite精灵图片
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// UISprite sprite = gameObject.GetComponent<UISprite>();
Ray myRay = Camera.main.ScreenPointToRay(Input.mousePosition); //建立射线 起点为摄像机 终点为鼠标的位置
RaycastHit hit; //发射射线返回信息
if(Input.GetMouseButtonDown(0)) //点击鼠标左键
{
if(Physics.Raycast(myRay,out hit)) //发射射线(起点终点,返回信息)
{
if (hit.collider.gameObject.tag == "Untagged") //如果射线信息碰撞标签为"ground"的物体
{
//sprite.spriteName =hit.collider.gameObject<>();
obj.spriteName = hit.collider.gameObject.GetComponent<UIButton>().normalSprite; //开始调用被射线碰撞的物体的UIButton组件里的NormalSprite
}
}
}
}
}
切记:替换和被替换的贴图必须在同一个图集中