コード例 #1
0
        /// <summary>
        /// Copy or paste settings to selected PlacementSettings
        /// </summary>
        /// <param name="loadout">The loadout that got clicked</param>
        /// <param name="copy">do we copy or paste ?</param>
        /// <param name="selected">the list of currently selected PlacementSettings for edition in the current PrefabPalette</param>
        private void CopyPasteSettings(LoadoutInfo loadout, bool copy, HashSet <int> selected)
        {
            if (copy)
            {
                copyPastePrefabSettings = loadout;
            }
            else
            {
                if (copyPastePrefabSettings == null)
                {
                    return;
                }

                PrefabPaletteEditor sourceEditor = prefabPaletteEditors[copyPastePrefabSettings.palette];
                PrefabPaletteEditor destEditor   = prefabPaletteEditors[loadout.palette];
                SerializedProperty  srcPAS       = sourceEditor.prefabs.GetArrayElementAtIndex(loadout.palette.FindIndex(loadout.prefab));
                SerializedProperty  srcPS        = srcPAS.FindPropertyRelative("settings");
                destEditor.serializedObject.Update();
                foreach (int i in selected)
                {
                    SerializedProperty destPAS = destEditor.prefabs.GetArrayElementAtIndex(i);
                    SerializedProperty destPS  = destPAS.FindPropertyRelative("settings");
                    PlacementSettings.CopySerializedProperty(srcPS, destPS);
                }
                destEditor.serializedObject.ApplyModifiedProperties();
            }
        }
コード例 #2
0
        void PlaceGameObject(PolyRaycastHit hit, PrefabAndSettings prefabAndSettings, BrushTarget target, BrushSettings settings)
        {
            if (prefabAndSettings == null)
            {
                return;
            }

            GameObject prefab = prefabAndSettings.gameObject;

            var worldPosition = target.transform.TransformPoint(hit.position);
            var worldNormal   = target.transform.TransformDirection(hit.normal);
            Ray ray           = RandomRay(worldPosition, worldNormal, settings.radius, settings.falloff, settings.falloffCurve);

            ray.origin    = target.transform.InverseTransformPoint(ray.origin);
            ray.direction = target.transform.InverseTransformDirection(ray.direction);

            PolyRaycastHit rand_hit;

            Vector3[] vertices  = target.editableObject.editMesh.vertices;
            int[]     triangles = target.editableObject.editMesh.GetTriangles();

            if (PolySceneUtility.MeshRaycast(ray, vertices, triangles, out rand_hit))
            {
                PlacementSettings placementSettings = prefabAndSettings.settings;
                Vector3           scaleSetting      = prefab.transform.localScale;
                if (placementSettings.uniformBool)
                {
                    float uniformScale = Random.Range(placementSettings.uniformScale.x, placementSettings.uniformScale.y);
                    scaleSetting *= uniformScale;
                }
                else
                {
                    if (placementSettings.xScaleBool)
                    {
                        scaleSetting.x = Random.Range(placementSettings.scaleRangeMin.x, placementSettings.scaleRangeMax.x);
                    }
                    if (placementSettings.yScaleBool)
                    {
                        scaleSetting.y = Random.Range(placementSettings.scaleRangeMin.y, placementSettings.scaleRangeMax.y);
                    }
                    if (placementSettings.zScaleBool)
                    {
                        scaleSetting.z = Random.Range(placementSettings.scaleRangeMin.z, placementSettings.scaleRangeMax.z);
                    }
                }

                Vector3 rotationSetting = Vector3.zero;
                if (placementSettings.xRotationBool)
                {
                    rotationSetting.x = Random.Range(placementSettings.rotationRangeMin.x, placementSettings.rotationRangeMax.x);
                }
                if (placementSettings.yRotationBool)
                {
                    rotationSetting.y = Random.Range(placementSettings.rotationRangeMin.y, placementSettings.rotationRangeMax.y);
                }
                if (placementSettings.xRotationBool)
                {
                    rotationSetting.z = Random.Range(placementSettings.rotationRangeMin.z, placementSettings.rotationRangeMax.z);
                }


                Quaternion rotation = Quaternion.FromToRotation(Vector3.up, target.transform.TransformDirection(rand_hit.normal));

                GameObject inst = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
                inst.transform.position   = target.transform.TransformPoint(rand_hit.position);
                inst.transform.rotation   = rotation;
                inst.transform.localScale = scaleSetting;

                float pivotOffset = s_UsePivotForPlacement ? 0f : GetPivotOffset(inst);

                inst.name = FormatInstanceName(prefab);

                inst.transform.position = inst.transform.position - (inst.transform.up * pivotOffset);
                inst.transform.rotation = inst.transform.rotation * Quaternion.Euler(rotationSetting);


                if (s_AvoidOverlappingGameObjects && TestIntersection(inst))
                {
                    Object.DestroyImmediate(inst);
                    return;
                }

                if (s_ParentObjectWithSurface)
                {
                    inst.transform.SetParent(target.transform);
                }

                m_PrefabsInstances.Add(inst);

                Undo.RegisterCreatedObjectUndo(inst, UndoMessage);
            }
        }
コード例 #3
0
        /// <summary>
        /// Draw everything concerning a single Prefab Palette in the Polybrush Window
        /// </summary>
        /// <param name="thumbSize">size of the preview textures</param>
        internal void OnInspectorGUI_Internal(int thumbSize)
        {
            PolyGUILayout.Label(m_GCCurrentPaletteLabel);

            serializedObject.Update();
            int  count        = prefabs != null ? prefabs.arraySize : 0;
            Rect dropDownZone = EditorGUILayout.BeginVertical(paletteStyle);

            dropDownZone.width = EditorGUIUtility.currentViewWidth;
            Rect backGroundRect = new Rect(dropDownZone);

            if (count != 0)
            {
                const int pad  = 4;
                int       size = thumbSize + pad;
                backGroundRect.x += 8;
                backGroundRect.y += 4;
                // The backgroundRect is currently as wide as the current view.
                // Adjust it to take the size of the vertical scrollbar and padding into account.
                backGroundRect.width -= (20 + (int)GUI.skin.verticalScrollbar.fixedWidth);
                // size variable will not take into account the padding to the right of all the thumbnails,
                // therefore it needs to be substracted from the width.
                int container_width = ((int)Mathf.Floor(backGroundRect.width) - (pad + 1));
                int columns         = (int)Mathf.Floor(container_width / size);
                int rows            = count / columns + (count % columns == 0 ? 0 : 1);
                if (rows < 1)
                {
                    rows = 1;
                }

                backGroundRect.height = 8 + rows * thumbSize + (rows - 1) * pad;
                EditorGUI.DrawRect(backGroundRect, EditorGUIUtility.isProSkin ? PolyGUI.k_BoxBackgroundDark : PolyGUI.k_BoxBackgroundLight);

                int currentIndex = 0;
                for (int i = 0; i < rows; i++)
                {
                    var horizontalRect = EditorGUILayout.BeginHorizontal();
                    for (int j = 0; j < columns; j++)
                    {
                        GUILayout.Space(pad);
                        var prefab          = prefabs.GetArrayElementAtIndex(currentIndex);
                        var previewRectXPos = pad + j * size + horizontalRect.x;
                        DrawPrefabPreview(prefab, currentIndex, thumbSize, previewRectXPos, horizontalRect.y);
                        currentIndex++;
                        if (currentIndex >= count)
                        {
                            break;
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                    GUILayout.Space(pad);
                }

                EditorGUILayout.EndVertical();

                if (selected.Count > 0)
                {
                    EditorGUILayout.LabelField(m_GCPlacementSettingsLabel);
                    GUILayout.Space(pad);
                }

                EditorGUILayout.BeginVertical();

                foreach (var i in selected)
                {
                    DrawSinglePrefabPlacementSettings(prefabs.GetArrayElementAtIndex(i), i);
                }

                /// Little Hack to get the Rect for dropping new prefabs
                Rect endDropDownZone = EditorGUILayout.BeginVertical();
                dropDownZone.height = endDropDownZone.y - dropDownZone.y;
                EditorGUILayout.EndVertical();
            }
            else
            {
                dropDownZone.height = thumbSize;
                var r = EditorGUILayout.BeginVertical(GUILayout.Height(thumbSize + 4));
                EditorGUI.DrawRect(r, EditorGUIUtility.isProSkin ? PolyGUI.k_BoxBackgroundDark : PolyGUI.k_BoxBackgroundLight);
                GUILayout.FlexibleSpace();
                GUILayout.Label("Drag Prefabs Here!", EditorStyles.centeredGreyMiniLabel);
                GUILayout.FlexibleSpace();
                EditorGUILayout.EndVertical();
            }
            EditorGUILayout.EndVertical();

            Event e = Event.current;

            if (dropDownZone.Contains(e.mousePosition) &&
                (e.type == EventType.DragUpdated || e.type == EventType.DragPerform) && DragAndDrop.objectReferences.Length > 0)
            {
                if (PolyEditorUtility.ContainsPrefabAssets(DragAndDrop.objectReferences))
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                }
                else
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
                }

                if (e.type == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();

                    IEnumerable <GameObject> dragAndDropReferences = DragAndDrop.objectReferences
                                                                     .Where(x => x is GameObject && PolyEditorUtility.IsPrefabAsset(x)).Cast <GameObject>();

                    foreach (GameObject go in dragAndDropReferences)
                    {
                        prefabs.InsertArrayElementAtIndex(prefabs.arraySize);
                        SerializedProperty last       = prefabs.GetArrayElementAtIndex(prefabs.arraySize - 1);
                        SerializedProperty gameObject = last.FindPropertyRelative("gameObject");
                        gameObject.objectReferenceValue = go;
                        PlacementSettings.PopulateSerializedProperty(last.FindPropertyRelative("settings"));
                        last.FindPropertyRelative("name").stringValue = go.name;
                    }
                }
            }

            if (e.type == EventType.KeyUp)
            {
                if (IsDeleteKey(e) && !GUI.GetNameOfFocusedControl().Contains("cancelbackspace"))
                {
                    PrefabPalette t = target as PrefabPalette;
                    t.RemoveRange(selected.ToArray());

                    selected.Clear();

                    if (onSelectionChanged != null)
                    {
                        onSelectionChanged(null);
                    }

                    PolybrushEditor.DoRepaint();
                }
            }

            serializedObject.ApplyModifiedProperties();
            redrawCounter += 1;
        }
コード例 #4
0
 internal PrefabAndSettings(GameObject go)
 {
     gameObject = go;
     name       = go.name;
     settings   = new PlacementSettings();
 }