unity实现浇水倒水效果

Android端代码:

 //-----------------------------------倾倒  开始------------------------------------------- 
    //开始倾倒,被unity调用
    public void kaiShiQingDao(){    
        //开始监听倾倒功能
        sensorManager.registerListener(listener, sensor,
                SensorManager.SENSOR_DELAY_NORMAL);
        //Toast.makeText(this, "kai shi qing dao", Toast.LENGTH_SHORT).show();
    }   
    //结束倾倒,被unity调用
    public void tingZhiQingDao(){
        if(sensorManager!=null)
        sensorManager.unregisterListener(listener);
    }
    //android调用unity,告诉unity当前手机的倾斜状态   
    public void qingDaoValue(String va,String vb){  //va表示左右,vb表示顶底
        UnityPlayer.UnitySendMessage("Main Camera", "getQingXieState", va+","+vb);
    }

    private SensorEventListener listener = new SensorEventListener() {
        @Override
        public void onAccuracyChanged(Sensor arg0, int arg1) {

        }   
        @Override
        public void onSensorChanged(SensorEvent event) {
            String topBottomString = "";
            String leftRightString = "";

            if (event.values[1] == 0) {
                //topBottomString = "手机头部或底部没有翘起";
                topBottomString=event.values[1]+"";
            } else if (event.values[1] > 0) {
                //topBottomString = "底部向上翘起" + event.values[1] + "度";
                topBottomString=event.values[1]+"";
            } else if (event.values[1] < 0) {   
                //topBottomString = "顶部向上翘起" + Math.abs(event.values[1]) + "度";
                topBottomString=event.values[1]+"";
            }

            if (event.values[2] == 0) {
                //leftRightString = "手机左侧或右侧没有翘起";
                leftRightString=event.values[2]+"";
            } else if (event.values[2] > 0) {
                //leftRightString = "右侧向上翘起" + event.values[2] + "度";
                leftRightString=event.values[2]+"";
            } else if (event.values[2] < 0) {
                //leftRightString = "左侧向上翘起" + Math.abs(event.values[2]) + "度";
                leftRightString=event.values[2]+"";
            }
                qingDaoValue(leftRightString,topBottomString);  
            //textView02.setText("手机顶部或尾部翘起的角度:  " + topBottomString);
            //textView03.setText("手机左侧或右侧翘起的角度:  " + leftRightString);
        }
    };  
    //----------------------------------------倾倒   结束--------------------------------------


unity端:

    public GameObject obj; //水壶
    Vector3 initAngles; //水壶初始的角度
    // Use this for initialization
    void Start () {
        initAngles = obj.transform.localEulerAngles; //记录水浒初始角度
        kaiShiQingDao(); //开始倾倒 
    }
    /
    /开始倾倒,被unity调用
    public void kaiShiQingDao()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic("currentActivity");
        jo.Call("kaiShiQingDao");  
    }   
      //结束倾倒,被unity调用
    public void tingZhiQingDao()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic("currentActivity");
        jo.Call("tingZhiQingDao");  
    }
      //android调用unity,告诉unity当前手机的倾斜状态
    public void getQingXieState(string va)
    {
        //逗号分离,第一个值表示左右倾倒,第二个值表示上下倾倒
        string[] zhi = va.Split(',');
        float v01 = Mathf.Abs(float.Parse(zhi[0]));
        float v02 = Mathf.Abs(float.Parse(zhi[1]));
        float lastValue= Mathf.Max(v01, v02);
        float zz = Mathf.Clamp(lastValue, 0, 75);
        obj.transform.localEulerAngles = new Vector3(initAngles.x, initAngles.y, initAngles.z-zz);
    }

unity实现浇水倒水效果_第1张图片


FR:海涛高软(QQ技术交流群)

你可能感兴趣的:(Unity)