unity 中关于鼠标悬停 不同于之前写的文章版本 这个更加实用

来源:互联网 发布:linux cpu 核数 编辑:程序博客网 时间:2024/06/08 00:35

本文章已现在流行的麻将为例   当游戏开始后  手指按在自己的麻将上会出现跟手按在钢琴上一样的效果 1,  被按住的麻将会上升起来  其他麻将都会落下  2,上升起来的麻将按住 会出牌(这里已销毁这个选中的麻将为例 作为出牌的效果)

我在啰嗦几句 第一这个脚本挂在需要相应的游戏体上  第二被挂游戏体必须带有collider, 第三仅仅制作完上面的两步 本应该没有问题,,笔者又发现一个问题 就是只有鼠标在物体的右上方才会很灵敏的相应到 在在左下方反而没什么反应 ,为此笔者在脚本上加上了一句对物体的碰撞体扩大范围

this.GetComponent<BoxCollider> ().size = new Vector3 (1f, 1.5f, 1.5f);


首先我们需要建立两个脚本  GameMain.cs (挂在主摄像机上)  与   Shubiaoxuanting.cs(挂在麻将上--让谁实现这个效果就挂在谁的身上)

首先是GameMain.cs 脚本里的内容  这脚本的主要目的是使用 arraylist数组        (有同学要问为什么,不能将其放在麻将本身上?因为我使用发生的错误--可能是因为我是新手的原因)

using System.Collections;using System.Collections.Generic;using UnityEngine;public class GameMian : MonoBehaviour {public static GameMian insdence;public  ArrayList mj_array=new ArrayList () ;public  GameObject lase_mj;void Awake(){insdence = this;}void Start () {}// Update is called once per framevoid Update () {}}



下面是 Shubiaoxuanting.cs(挂在麻将上--让谁实现这个效果就挂在谁的身上)

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Shubiaoxuanting : MonoBehaviour{bool ischanger=false;//上移动   0为未上移  1为上移int move_up_fuzhu=0;void Start () {//扩大碰撞体的尺寸 方便检测鼠标悬停this.GetComponent<BoxCollider> ().size = new Vector3 (1f, 1.5f, 1.5f);//给麻将初始状态的位置this.transform.position = new Vector3 (this.transform.position.x, 0.3f, this.transform.position.z);}void OnMouseEnter(){//因为GameMian.insdence.lase_mj一开始为空的  所以需要一个状态让它第一次之后才开启foreach(GameObject mj_ in GameMian.insdence.mj_array){if(this.gameObject!=mj_){GameMian.insdence.lase_mj.transform.position = new Vector3 (GameMian.insdence.lase_mj.transform.position.x, 0.3f, GameMian.insdence.lase_mj.transform.position.z);}}//将选中的麻将提高this.transform.position = new Vector3 (this.transform.position.x, 0.5f, this.transform.position.z);GameMian.insdence.lase_mj = this.gameObject;GameMian.insdence.mj_array.Clear ();GameMian.insdence.mj_array.Add (GameMian.insdence.lase_mj);}//鼠标不再上面引起的动作//void OnMouseExit(){//if (move_up_fuzhu == 0) {//this.transform.position = new Vector3 (this.transform.position.x, 0.3f, this.transform.position.z);//}////}void OnMouseDown() {Debug.Log("点击了");move_up_fuzhu = 1;if (this.transform.position == new Vector3 (this.transform.position.x, 0.5f, this.transform.position.z)) {Destroy (this.gameObject);GameMian.insdence.mj_array.Clear ();}}void Update () {}}


好了 我们的效果实现了

阅读全文
1 0
原创粉丝点击