Example #1
0
    private void AddImageAnchor(ARImageAnchor arImageAnchor)
    {
        //Debug.LogFormat("Anchor added[{0}] : tracked {1}, name '{2}'", arImageAnchor.Identifier, arImageAnchor.IsTracked, arImageAnchor.ReferenceImageName);
        int index;

        if (arImageAnchor.ReferenceImageName != null && int.TryParse(arImageAnchor.ReferenceImageName, out index) && index >= 0)
        {
            TriggerObject triggerObject;
            var           triggerObjects = TriggerObjects;
            if (triggerObjects != null && triggerObjects.TryGetValue(index, out triggerObject))
            {
                //Debug.LogFormat("Index {0} trigger object {1}", index, triggerObject != null);

                while (index >= _gameObjects.Count)
                {
                    _gameObjects.Add(null);
                }

                var arObjectState = ArBehaviour.ArObjectState;
                if (arObjectState != null && _gameObjects[index] == null)
                {
                    lock (arObjectState)
                    {
                        GameObject newGameObject;
                        var        result = ArBehaviour.CreateArObject(
                            arObjectState,
                            triggerObject.gameObject,
                            null,
                            transform,
                            triggerObject.poi,
                            triggerObject.poi.id,
                            out newGameObject
                            );
                        if (!ArBehaviourPosition.IsEmpty(result))
                        {
                            ArBehaviour.ErrorMessage = result;
                            return;
                        }
                        _gameObjects[index] = newGameObject;
                        newGameObject.SetActive(true);
                    }
                }
            }
        }
    }
Example #2
0
    private void UpdateImageAnchor(ARImageAnchor arImageAnchor)
    {
        //Debug.LogFormat("Anchor updated[{0}] : tracked {1}, name '{2}' GOs {3}",
        //    arImageAnchor.Identifier, arImageAnchor.IsTracked, arImageAnchor.ReferenceImageName, _gameObjects.Count);
        int index;

        if (arImageAnchor.ReferenceImageName != null && int.TryParse(arImageAnchor.ReferenceImageName, out index) && index >= 0)
        {
            GameObject gameObjectToAHandle = null;
            if (index < _gameObjects.Count)
            {
                gameObjectToAHandle = _gameObjects[index];
            }
            //Debug.LogFormat("Index {0} game object {1}", index, gameObjectToAHandle != null);
            if (gameObjectToAHandle != null)
            {
                if (arImageAnchor.IsTracked)
                {
                    gameObjectToAHandle.transform.localPosition = UnityARMatrixOps.GetPosition(arImageAnchor.Transform);
                    gameObjectToAHandle.transform.localRotation = UnityARMatrixOps.GetRotation(arImageAnchor.Transform);
                    if (!gameObjectToAHandle.activeSelf)
                    {
                        gameObjectToAHandle.SetActive(true);
                    }
                }
                else if (gameObjectToAHandle.activeSelf)
                {
                    //gameObjectToAHandle.SetActive(false);
                }
            }
            else if (_hasTriggerObjects)
            {
                //Debug.LogFormat("Anchor re-added[{0}] : tracked {1}, name '{2}'", arImageAnchor.Identifier, arImageAnchor.IsTracked, arImageAnchor.ReferenceImageName);
                if (arImageAnchor.ReferenceImageName != null && int.TryParse(arImageAnchor.ReferenceImageName, out index) && index >= 0)
                {
                    TriggerObject triggerObject;
                    var           triggerObjects = TriggerObjects;
                    if (triggerObjects != null && triggerObjects.TryGetValue(index, out triggerObject))
                    {
                        //Debug.LogFormat("Index {0} trigger object {1}", index, triggerObject != null);

                        while (index >= _gameObjects.Count)
                        {
                            _gameObjects.Add(null);
                        }

                        var arObjectState = ArBehaviour.ArObjectState;
                        if (arObjectState != null && _gameObjects[index] == null)
                        {
                            lock (arObjectState)
                            {
                                GameObject newGameObject;
                                var        result = ArBehaviour.CreateArObject(
                                    arObjectState,
                                    triggerObject.gameObject,
                                    null,
                                    transform,
                                    triggerObject.poi,
                                    triggerObject.poi.id,
                                    out newGameObject
                                    );
                                if (!ArBehaviourPosition.IsEmpty(result))
                                {
                                    ArBehaviour.ErrorMessage = result;
                                    return;
                                }
                                _gameObjects[index] = newGameObject;
                                newGameObject.SetActive(true);
                            }
                        }
                    }
                }
            }
        }
    }