Ejemplo n.º 1
0
        static void StartStamping()
        {
            Init();
            Object obj = Selection.activeObject;

            if (obj.GetType() == typeof(TextAsset))
            {
                // instance.Stamp = Serializer.Deserialize<StampData>((Selection.activeObject as TextAsset).text); // EasyJson plugin
                instance.Stamp = JsonUtility.FromJson <StampData>((Selection.activeObject as TextAsset).text);
            }
            else
            { // we will generate a stamp from this single prefab
                StampData  singleStamp = new StampData();
                StampChild singleChild = new StampChild();
                singleChild.PrefabPath = AssetDatabase.GetAssetPath(obj);
                singleStamp.prefabs.Add(singleChild);
                instance.Stamp = singleStamp;
            }
            currentStampName = Selection.activeObject.name;
        }
Ejemplo n.º 2
0
        static void CreateStamp()
        {
            if (Selection.gameObjects.Length == 0)
            {
                return;
            }

            // extra check: selected objects must be in scene
            if (Selection.activeGameObject.activeInHierarchy == true)
            {
                // In Hierarchy, ok
            }
            else
            {
                // In Project View, cannot use
                Debug.LogError("Select prefabbed objects from scene/hierarcy, not from the project folder..");
                return;
            }

            foreach (GameObject go in Selection.gameObjects)
            {
                if (!go.activeInHierarchy)
                {
                    continue;
                }

                GameObject parentPrefab = PrefabUtility.GetPrefabParent(go) as GameObject;

                if (parentPrefab == null || string.IsNullOrEmpty(AssetDatabase.GetAssetPath(parentPrefab)))
                {
                    Debug.LogError("All selected GameObjects must be prefabs to make a stamp.");
                    return;
                }
            }

            StampData stampData = new StampData();
            Vector3   center    = Vector3.zero;
            Bounds    bounds    = new Bounds();

            foreach (GameObject go in Selection.gameObjects)
            {
                if (!go.activeInHierarchy)
                {
                    continue;
                }
                StampChild sc = new StampChild();
                sc.PrefabPath  = AssetDatabase.GetAssetPath(PrefabUtility.GetPrefabParent(go));
                sc.position    = go.transform.position;
                sc.eulerAngles = go.transform.eulerAngles;
                sc.scale       = go.transform.localScale;
                center        += sc.position;
                // get bounds from all children
                foreach (Transform child in go.transform)
                {
                    var mf = child.GetComponent <MeshFilter>();
                    bounds.Encapsulate(mf.sharedMesh.bounds);
                }
                stampData.prefabs.Add(sc);
            }

            stampData.center = center / stampData.prefabs.Count;
            stampData.bounds = bounds;

            // string jsonString = Serializer.Serialize(stampData, true); // EasyJSON plugin
            string jsonString = JsonUtility.ToJson(stampData, true);

            string assetPath = stampDataFolder + "new stamp.JSON";
            string fullPath  = Application.dataPath + assetPath;

            if (Directory.Exists(Path.GetDirectoryName(fullPath)) == true)
            {
                StreamWriter stream = File.CreateText(fullPath);
                stream.WriteLine(jsonString);
                stream.Close();

                Debug.Log("Created new stamp at " + fullPath);

                AssetDatabase.Refresh();
            }
            else
            {
                Debug.LogError("Missing stamp data folder: " + fullPath);
            }
        }