示例#1
0
    private void OnMessage(object sender, MessageEventArgs e)
    {
        if (e.IsText)
        {
            var mode = ExtractJsonMode(e.Data);
            switch (mode)
            {
            case "PlaceObject":
                var placedData = JsonUtility.FromJson <PlacedObjectData>(e.Data);
                break;

            case "UpdateObject":
                var updatedData = JsonUtility.FromJson <UpdateObjectData>(e.Data);
                OnObjectUpdated?.Invoke(this, updatedData);
                break;

            default:
                Debug.LogError("Something wrong");
                break;
            }
        }
        else if (e.IsBinary)
        {
        }
    }
        /// <summary>
        /// Updates the products positions. If there are not enough products yet, produce the missing ones.
        /// If there are too many, deactivate the overlapping.
        /// </summary>
        /// <param name="positions">positions of objects to create or update</param>
        /// <param name="normals">normal directions where objects should look at. Overrides lookDir param. Can be null</param>
        /// <param name="lookDir">direction where objects should look at. Set to Vector3.zero to ignore</param>
        public void MassProduceOrUpdate(Vector3[] positions, Vector3[] normals, Vector3 lookDir)
        {
            if (positions.Length > objectLimit)
            {
                Array.Resize <Vector3>(ref positions, objectLimit);
            }
            for (int i = 0; i < positions.Length; i++)
            {
                GameObject product       = GetAt(i);
                bool       newlyProduced = false;

                if (product == null)
                { // Produce new product
                    product       = Produce(positions[i]);
                    newlyProduced = true;
                }
                else
                { // Update product
                    product.transform.position = CalcPos(positions[i]);
                    product.SetActive(true);
                    lastActiveIndex = i;
                }

                if (normals != null)
                {
                    lookDir = normals[i];
                }
                if (lookDir != Vector3.zero)
                {
                    product.transform.rotation = Quaternion.LookRotation(lookDir, upAxis);
                    if (objectEuler != Vector3.zero)
                    {
                        product.transform.eulerAngles += objectEuler;
                    }
                    else
                    {
                        product.transform.eulerAngles += objectToCreate.transform.eulerAngles;
                    }
                }

                if (newlyProduced)
                {
                    OnObjectProduced?.Invoke(product, i, positions.Length);
                    product = products[i];
                    OnObjectProducedLate?.Invoke(product, i, positions.Length);
                }
                else
                {
                    OnObjectUpdated?.Invoke(product, i, positions.Length);
                    product = products[i];
                    OnObjectUpdatedLate?.Invoke(product, i, positions.Length);
                }
            }

            // Deactivate overlapping products
            if (products.Count > positions.Length)
            {
                for (int i = positions.Length; i < products.Count; i++)
                {
                    products[i].SetActive(false);
                    lastActiveIndex = i - 1;
                }
            }
        }
示例#3
0
 private void InvokeObjectUpdated()
 {
     OnObjectUpdated?.Invoke(this, EventArgs.Empty);
 }
示例#4
0
 protected void OnUpdated(XapEventArgs e)
 {
     OnObjectUpdated?.Invoke(this, e);
 }