unity3D 简单脚本沟通

写在前面:
没有系统学过unity和C#,感觉在稍微复杂的问题面前就会方寸大乱,没有头绪。打算在1月补一下基础,感觉越到后面会越头秃,毕竟不能只靠博客和论坛学习吧。

正文:
我的需求是当血条加满时触发人物动画,也就是博客上常说的脚本之间的信息交互。
我一直是用arduino代码的视角去看C#,所以在这种问题上老是走死胡同。
我的人物动画用的是animator,也就是在设置动画过程中要用到状态机:
在这里插入图片描述

这个小东西。
关于animation和animator之间有什么不同的问题,我以前没想过,这次做的时候才不得不查了一下:animator可以通过状态机让动画之间有一定逻辑。

然后核心部分也比较清楚:血没加满→动画暂停;血加满了→动画播放。
代码示例:

(因为我的血条是由硬件控制的,VL53L0X测距传感器和arduino uno板,实时反馈距离信息,所以会出现血满了或者掉了变化急剧的现象,其实是我改变了障碍物距离【笑】)

血条:

using UnityEngine;
using System.Collections;
using System.IO.Ports;
using UnityEngine.UI;

public class Depthbar : MonoBehaviour
{
    public Image HealthBar ;
    public SerialPort sp = new SerialPort("COM9", 9600);
    //public void SendMessage;
    //public GameObject go1;
    public int value;
    //public GameObject human;


    void Start()
    {
        sp.Open();
        sp.ReadTimeout = 1;

    }
    void Update()
    {
        if (sp.IsOpen)
        {
            try
            {
                SetDepthBar(sp.ReadByte());
                print(sp.ReadByte());
                //human.SendMessage("Standup", "Direction", SendMessageOptions.RequireReceiver);

            }
            catch (System.Exception)
            //此类是作为一种区分系统异常和应用程序异常的手段提供的。
            { }
        }
    }

    void SetDepthBar(int Direction)
    {
        if (Direction == 0)
        {
            HealthBar.fillAmount = 0.0f;
        }

        if (Direction == 1)
        {
            HealthBar.fillAmount = 0.0f;
        }

        if (Direction == 2)
        {
            HealthBar.fillAmount = 0.2f;
        }

        if (Direction == 3)
        {
            HealthBar.fillAmount = 0.3f;
        }

        if (Direction == 4)
        {
            HealthBar.fillAmount = 0.4f;
        }

        if (Direction == 5)
        {
            HealthBar.fillAmount = 0.5f;
        }

        if (Direction == 6)
        {
            HealthBar.fillAmount = 0.6f;
        }

        if (Direction == 7)
        {
            HealthBar.fillAmount = 0.7f;
        }

        if (Direction == 8)
        {
            HealthBar.fillAmount = 0.8f;
        }

        if (Direction == 9)
        {
            HealthBar.fillAmount = 0.9f;
        }

        if (Direction == 10)
        {
            HealthBar.fillAmount = 1.0f;
           
        }
    }

  
}

人物:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;




public class ReceiveMessage : MonoBehaviour
{
    //public Animation anim;
    public Animator animator;
    public float number=0f;
    //public GameObject Image;
    public Image HealthBar;

    void Start()
    {
        animator = GetComponent <Animator>();
        //HealthBar = Image.GetComponent<Image>();
    }

    private void Update()
    {
        number = HealthBar.fillAmount;
        print(number);
        Standup(number);
    }

    void Standup(float number)
    {
        if (number == 1.0f)
        {
            animator.speed = 1.0f;
        }
        else
        {
            animator.speed = 0.0f;
        }
    }
    



   
}

中间的注释掉的语句是我走过的弯路,没有别的意思。

成品截图:
在这里插入图片描述

2018最新微信墙微信上墙婚庆会议大屏幕3D签到抽奖摇一摇微信上墙源码。这是某人在淘宝上面花了200大洋买的,php版本5.4以上,亲测可用,上墙,抽奖,摇一摇。都可以使用。绝对没有问题。之前在网上花了2天时间也找了一些类似的,有免费下载的,也有包括在csdn上面用5个资源分下载类似的,发现都不能用,至少我在本地或有服务器上都没有调试成功,看了一下他们的代码,发现代码用的是公众号帐号和密码,来模拟微信登陆来获取用户信息。微信已经在2016年就已经停止模似登陆了,登陆微信公众号,必须要扫描后,管理员确认后才可登陆。所以,那些代码都不能用。有些有csdn上传的这些代码基本上不能用(可能2015年前可以用,但是现在不能用),感觉太不负责任了,都是骗积分的。当时因为公司年会要弄个微信上墙及现在抽奖活动,找这个代码也很辛苦,相信大家也一样辛苦,所以,把代码放出来,供大家下载学习使用。这个代码,我是在淘宝上买的,本身内置了第三方的微信公众号授权,所以,如果你的公众号没有微信认证也可以用,直接扫码就可以发送微信上墙弹幕,不会出现第三方公众号信息。如果你的微信公众号认证了,就用自己的,后台可以配置,填写你的公众号appid和appsecret即可。好了,不多说了,大家 下 载使用,如果不能用,我退积分。不清楚的,大家可以在评论区留言。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值