Ejemplo n.º 1
0
    private void Awake()
    {
        _objectConfigModule   = ModuleManager.Instance.Get <ObjectsConfigModule>();
        _assemblyObjectModule = ModuleManager.Instance.Get <AssemblyObjectsModule>();

        _leftGrab  = VRTK_DeviceFinder.GetControllerLeftHand().GetComponent <VRTK_InteractGrab>();
        _rightGrab = VRTK_DeviceFinder.GetControllerRightHand().GetComponent <VRTK_InteractGrab>();
        string name = gameObject.name.Replace("(Clone)", "");

        _id = _objectConfigModule.GetObjectInfoIDByName(name);
        _assemblyObjectModule.RegisterAssemblyObjectTransform(_id, transform);
        _jsonAssemblyObject = _assemblyObjectModule.GetJsonAssemblyObjectByID(_id);
        InitialChildPos(out _ID_HasPos_List);
    }
Ejemplo n.º 2
0
    private void SetToValidParentPosition()
    {
        var rigidBody = transform.GetComponent <Rigidbody>();

        for (int i = 0; i < _jsonAssemblyObject.parentIDs.Length; ++i)
        {
            int parentID = _jsonAssemblyObject.parentIDs[i];
            JsonAssemblyObject jsonParent  = _assemblyObjectModule.GetJsonAssemblyObjectByID(parentID);
            Transform[]        parentTrans = _assemblyObjectModule.GetTransformWithoutSelfByID(parentID, transform);
            if (parentTrans == null)
            {
                return;
            }
            for (int j = 0; j < parentTrans.Length; ++j)
            {
                JsonChildPosInfo[] childPosInfos = jsonParent.jsonChildPosInfos;
                if (childPosInfos == null)
                {
                    Debug.LogError("The childPosInfo in null,This is Error!");
                    return;
                }
                for (int k = 0; k < childPosInfos.Length; ++k)
                {
                    if (childPosInfos[k].childID == _id && parentTrans[j].GetComponent <AssemblyObject>().IsHasChildPos(_id, k))
                    {
                        Vector3 validPos = parentTrans[j].TransformPoint(childPosInfos[k].JsonChildPos.ToVector3());
                        if (Vector3.Distance(validPos, transform.position) < 0.07f)
                        {
                            transform.parent           = parentTrans[j];
                            transform.localPosition    = childPosInfos[k].JsonChildPos.ToVector3();
                            transform.localEulerAngles = childPosInfos[k].JsonChildRot.ToVector3();
                            transform.localScale       = childPosInfos[k].JsonChildScal.ToVector3();
                            parentTrans[j].GetComponent <AssemblyObject>().GetAChildPos(k, _id);
                            rigidBody.isKinematic = true;
                            return;
                        }
                    }
                }
            }
        }
        transform.parent      = null;
        rigidBody.isKinematic = false;
    }
Ejemplo n.º 3
0
    private void ReadJsonDataFromLocal(string fileName)
    {
        string parth = Application.streamingAssetsPath + '/' + fileName + ".json";

        if (File.Exists(parth))
        {
            FileStream   fs    = new FileStream(parth, FileMode.Open);
            StreamReader sr    = new StreamReader(fs);
            string[]     lines = sr.ReadToEnd().Split('\n');
            for (int i = 0; i < lines.Length - 1; ++i)
            {
                JsonAssemblyObject jsonLabObject = JsonMapper.ToObject <JsonAssemblyObject>(lines[i]);
                _ID_JsonAssembyObject_Dic.Add(jsonLabObject.ID, jsonLabObject);
            }
            fs.Close();
            fs.Dispose();
        }
        else
        {
            Debug.LogError("Can't Find " + fileName + ".json in StreamingAssets");
        }
    }
Ejemplo n.º 4
0
    private static void CreateLabObjectJsonFile()
    {
        ReadObjectsConfigFile();
        _ID_JsonAssemObject_Dic.Clear();

        GameObject[] labobjects = GameObject.FindGameObjectsWithTag(LabObjectTag);
        for (int i = 0; i < labobjects.Length; ++i)
        {
            if (_Name_ID_Dic.ContainsKey(labobjects[i].name) && labobjects[i].CompareTag(LabObjectTag) && labobjects[i].activeInHierarchy)
            {
                int id = _Name_ID_Dic[labobjects[i].name];

                string parentName = (labobjects[i].transform.parent == null ? null : labobjects[i].transform.parent.CompareTag(LabObjectTag) ? labobjects[i].transform.parent.name : null);
                int    parentID   = (parentName == null ? -1 : _Name_ID_Dic.ContainsKey(parentName) ? _Name_ID_Dic[parentName] : -1);

                if (_ID_JsonAssemObject_Dic.ContainsKey(id))
                {
                    if (parentID == -1)
                    {
                        if (_ID_JsonAssemObject_Dic[id].jsonWorldTransforms != null)
                        {
                            List <JsonWorldTransform> list = new List <JsonWorldTransform>(_ID_JsonAssemObject_Dic[id].jsonWorldTransforms);
                            list.Add(new JsonWorldTransform(labobjects[i].transform));
                            _ID_JsonAssemObject_Dic[id].jsonWorldTransforms = list.ToArray();
                        }
                        else
                        {
                            _ID_JsonAssemObject_Dic[id].jsonWorldTransforms = new JsonWorldTransform[1] {
                                new JsonWorldTransform(labobjects[i].transform)
                            };
                        }
                    }
                    else
                    {
                        if (_ID_JsonAssemObject_Dic[id].parentIDs != null)
                        {
                            HashSet <int> set = new HashSet <int>(_ID_JsonAssemObject_Dic[id].parentIDs);
                            set.Add(parentID);
                            _ID_JsonAssemObject_Dic[id].parentIDs = new int[set.Count];
                            set.CopyTo(_ID_JsonAssemObject_Dic[id].parentIDs);
                        }
                        else
                        {
                            _ID_JsonAssemObject_Dic[id].parentIDs = new int[1] {
                                parentID
                            };
                        }
                    }
                }
                else
                {
                    JsonAssemblyObject jsonAssemObj = new JsonAssemblyObject();
                    jsonAssemObj.ID   = id;
                    jsonAssemObj.Name = labobjects[i].name;

                    if (parentID == -1)
                    {
                        List <JsonWorldTransform> list = new List <JsonWorldTransform>();
                        list.Add(new JsonWorldTransform(labobjects[i].transform));
                        jsonAssemObj.jsonWorldTransforms = list.ToArray();
                    }
                    else
                    {
                        jsonAssemObj.parentIDs = new int[1] {
                            parentID
                        };
                    }
                    _ID_JsonAssemObject_Dic.Add(id, jsonAssemObj);
                }

                List <JsonChildPosInfo> jsonChildPosInfo = new List <JsonChildPosInfo>();
                for (int j = 0; j < labobjects[i].transform.childCount; ++j)
                {
                    Transform child = labobjects[i].transform.GetChild(j);
                    if (_Name_ID_Dic.ContainsKey(child.name) && child.CompareTag(LabObjectTag) && child.gameObject.activeInHierarchy)
                    {
                        int childID = _Name_ID_Dic[child.name];
                        jsonChildPosInfo.Add(new JsonChildPosInfo(childID, child.transform));
                    }
                }

                if (_ID_JsonAssemObject_Dic[id].jsonChildPosInfos == null && jsonChildPosInfo.Count > 0)
                {
                    _ID_JsonAssemObject_Dic[id].jsonChildPosInfos = jsonChildPosInfo.ToArray();
                }
                else
                {
                    if (_ID_JsonAssemObject_Dic[id].jsonChildPosInfos == null && jsonChildPosInfo.Count == 0)
                    {
                        continue;
                    }
                    if (_ID_JsonAssemObject_Dic[id].jsonChildPosInfos.Length == jsonChildPosInfo.Count)
                    {
                        for (int j = 0; j < jsonChildPosInfo.Count; ++j)
                        {
                            if (_ID_JsonAssemObject_Dic[id].jsonChildPosInfos[j].childID != jsonChildPosInfo[j].childID)
                            {
                                Debug.LogError("The same id AssemblyObject has different childRen,this is not permit!");
                                return;
                            }
                            else
                            {
                                if (Vector3.Distance(_ID_JsonAssemObject_Dic[id].jsonChildPosInfos[j].JsonChildPos.ToVector3(), jsonChildPosInfo[j].JsonChildPos.ToVector3()) > 0.0000001f)
                                {
                                    Debug.LogError("The same id AssemblyObject has different childRen,this is not permit!");
                                    return;
                                }
                            }
                        }
                    }
                    else
                    {
                        Debug.LogError("The same id AssemblyObject has different childRen,this is not permit!");
                        return;
                    }
                }
            }
        }

        SaveToJson("AssemblyObjectJsonFile");
    }