服务器数据处理

需求:

  1. 实时获取服务器数据。
  2. 数据进行处理(细化数据,假设从服务器加载一个数据,这个数据为1,并且已知是三维坐标里面某个轴的坐标,我现在需要移动至1这个位置,不许使用Vector3.Lerp(),Vector.MoveTowrads(),)。
  3. 现实与虚拟进行近同步。

思路:

  1. 把服务器传入的数据进行拆分,本地定义一个本地数据的标准值,从服务器得道的数据除以本地标准化数据,得道一个长度,这个长度所代表的是如果按照我本地的标准值,从服务器加载下来的数据我可以拆成多少份, 然后把这个长度进行记录,所记录的长度乘上本地标准的数据就是从服务器加载的数据了。我们需要用到这些数据时,需要把之前记录的数据索引作为一轮循环的长度,然后以本地标准化的数据传给作用物体,我需要传多少次才能够等于从服务器加载下来的初始数据。但是也存在数据丢失的问题。

关于数据丢失的问题,需要进行改进可解决

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
 *data type [0(x),1(z),...] of  struct
 */
public struct Vector
{
    public float x;
    public float z;
    public Vector(float x, float z)
    {
        this.x = x;
        this.z = z;
    }
}

/// <summary>
/// get name is data of json data content
/// </summary>
public class GetJsonData : MonoBehaviour
{

    public class JsonData { public List<float> m_coord = new List<float>(); }
    protected string path = "E:/SVN_Project/UnityProject/ChengLiang_2017.1.1/Assets/Data/data.txt";
    private static int i;
    private int tempCount1 = 0;
    private int tempCount2 = 0;
    public static List<int> m_indexPile_x = new List<int>();
    public static List<int> m_indexPile_Z = new List<int>();
    static public float normDataSpoce = 0.1f;
    void Start()
    {
        WWW w = new WWW(path);
        StartCoroutine(load(w));
    }

    //load json txt 
    IEnumerator load(WWW w)
    {
        yield return w;
        string txt = w.text;
        JsonData jd = JsonUtility.FromJson<JsonData>(txt);
        if (jd.m_coord.Count != 0)
            yield return StartCoroutine(OnSetupSendFixedTime(jd.m_coord));
        StartCoroutine(load(w));
    }
    string str;
    //setup data of fixed send time 
    IEnumerator OnSetupSendFixedTime(List<float> data)
    {
        Vector realTime;
        if (i < data.Count - 2)
        {
            str += data[i].ToString()+",";
            realTime = new Vector(data[i], data[i + 1]);
            OnDataDispose(realTime);
            yield return new WaitForSecondsRealtime(0.1f);
            i += 2;
        }
    }

    //json data to dispose 
    public void OnDataDispose(Vector realTime)
    {
        float rx = realTime.x;
        float rz = realTime.z;
        if (Mathf.Abs(rx) > normDataSpoce)
        {
            int count = (int)(rx / normDataSpoce);
            if (tempCount1 != count)
            {
                m_indexPile_x.Add(count);
                tempCount1 = count;
            }
        }
        if (Mathf.Abs(rz) > normDataSpoce)
        {
            int count = (int)(rz / normDataSpoce);
            if (tempCount2 != count)
            {
                m_indexPile_Z.Add(count);
                tempCount2 = count;
            }
        }
    }
}






using UnityEngine;
using System.Collections.Generic;


public class AGV_Controller : MonoBehaviour {
    #region [instance]
    static public AGV_Controller _instance;
    void Awake() { _instance = this; }
    #endregion
    private int index, index1;
    private Vector3 position;
    void Start() { position = transform.position; }
    void FixedUpdate()
    {
        OnSettingMoveData();
    }


    private void OnSettingMoveData()
    {
        GetDataInfo(GetJsonData._instance.m_indexPile_Z, GetJsonData._instance.normDataSpoce);
    }

    //get dispoce next of data info
    public void GetDataInfo(List<int> countPile, float normSpoce)
    {
        if (index < countPile.Count)
        {
            float m_Data =0.0f;
            for (int i = 0; i < Mathf.Abs(countPile[index]); i++)
            {
                if (countPile[index] > 0)
                    m_Data += normSpoce;
                else
                    m_Data -= normSpoce;
            }
            float moveQuantity = m_Data - transform.position.z;
            int _count = 0;
            if (Mathf.Abs(moveQuantity) > normSpoce)
                _count = (int)(moveQuantity / normSpoce);
            if (index1 < Mathf.Abs(_count))
            {
                if (_count > 0)
                    position.z += normSpoce;
                else
                    position.z -= normSpoce;
                index1++;
            }
            if (Mathf.Abs(_count)-index1 == 0)
            {
                index1 = 0;
                index++;
            }
            transform.position = position;
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值