public static bool isPrefab(UnityEngine.Object obj) { // Debug.Log("[PrefabUtil] prefab type:"+PrefabUtility.GetPrefabType(obj)+ " : "+ obj.name + " parent:"+PrefabUtility.GetPrefabObject(obj)); PrefabTypes p = PrefabUtil.GetPrefabType(obj); return(p == PrefabTypes.Prefab || p == PrefabTypes.PrefabVariant || p == PrefabTypes.NestedPrefab); }
/** * Provides a method to determine if the given property on the given prefab instance is a modification to the existing prefab locally (in a scene). */ public static bool isInstanceModification(SerializedProperty prop) { Object obj = prop.serializedObject.targetObject; PrefabTypes prefabType = PrefabUtil.GetPrefabType(obj); if (prefabType == PrefabTypes.PrefabInstance || prefabType == PrefabTypes.NestedPrefabInstance) { PropertyModification[] pms = PrefabUtility.GetPropertyModifications(obj); #if UNITY_2018_3_OR_NEWER UnityEngine.Object parent = PrefabUtility.GetOutermostPrefabInstanceRoot(obj); #else UnityEngine.Object parent = PrefabUtility.GetPrefabParent(obj); #endif if (pms.Length > 0) { foreach (PropertyModification pm in pms) { // Debug.Log("[PrefabUtil] pm:"+pm.propertyPath); if (pm.target == parent && pm.propertyPath.StartsWith(prop.propertyPath)) { return(true); } } } } return(false); }
public static GameObject getPrefabRoot(GameObject go) { PrefabTypes type = PrefabUtil.GetPrefabType(go); if (type == PrefabTypes.PrefabInstance) { #if UNITY_2018_3_OR_NEWER go = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(go); #else go = (GameObject)PrefabUtility.GetPrefabObject(go); #endif return(go); } #if UNITY_2018_3_OR_NEWER if (type == PrefabTypes.NestedPrefabInstance) { go = (GameObject)PrefabUtility.GetOutermostPrefabInstanceRoot(go); } #endif if (type == PrefabTypes.Prefab || type == PrefabTypes.PrefabVariant || type == PrefabTypes.NestedPrefab) { return(go.transform.root.gameObject); } return(null); }
/// <summary> /// Spawn the specified prefab, and add configuration components to /// newly created entities. Please note that if<see cref="cfg" /> /// is /// null, then no configuration component will be added to /// entities. /// </summary> /// <param name="type">type of prefab</param> /// <param name="count">number of entities to instantiate</param> /// <param name="cfg">configuration to those entities</param> public static bool Instantiate( PrefabTypes type, int count = 1, IConfigurable cfg = null ) { NativeArray <Entity> entities = new NativeArray <Entity>(count, Allocator.TempJob); if (!PrefabManager._cached) { PrefabManager.CachePrefabs(); } bool spawned = PrefabManager .SpawnPrefab((int)type, entities, cfg); // Try one more time if failed to spawn. // Just in case new prefabs were loaded after last cache. if (!spawned) { Debug.Log("PrefabCacheManager: cache miss"); PrefabManager.CachePrefabs(true); spawned = PrefabManager .SpawnPrefab((int)type, entities, cfg); } entities.Dispose(); return(spawned); }
public override void SerializeEffect <T>(T effect) { var comp = effect as Summon; if (effect is SummonAI) { SummonPrefabType = PrefabTypes.Resource; var name = comp.SummonedPrefab.name; if (name.EndsWith("(Clone)")) { name = name.Substring(0, name.Length - 7); } Prefab = name; } else { SummonPrefabType = PrefabTypes.Item; Prefab = comp.SummonedPrefab.gameObject.GetComponent <Item>().ItemID.ToString(); } BufferSize = comp.BufferSize; LimitOfOne = comp.LimitOfOne; SummonMode = comp.InstantiationMode; PositionType = comp.PositionType; MinDistance = comp.MinDistance; MaxDistance = comp.MaxDistance; SameDirectionAsSummoner = comp.SameDirAsSummoner; SummonLocalForward = comp.SummonLocalForward; IgnoreOnDestroy = comp.IgnoreOnDestroy; }
public void SpawnPrefab(PrefabTypes type, Vector3 pos, Quaternion rot = default(Quaternion)) { GameObject prefab = Instantiate(getPrefabByType(type)); prefab.transform.position = pos; prefab.transform.rotation = rot; }
public void Reset() { _type = PrefabTypes.CYLINDER; _shape = Shapes.CIRCLE; _amount = 200; _radius = 60; _sensitivity = 40; _speed = 5; }
public override void SearchGameObject(SearchJob job, GameObject go) { job.assetData.searchExecuted = true; PrefabTypes goType = PrefabUtil.GetPrefabType(go); if (goType == PrefabTypes.PrefabInstance || goType == PrefabTypes.NestedPrefabInstance || goType == PrefabTypes.PrefabVariantInstance) { GameObject goPrefabObj = null; #if UNITY_2018_3_OR_NEWER if (goType == PrefabTypes.PrefabInstance || goType == PrefabTypes.NestedPrefabInstance) { // nested prefabs or normal instances means we should search for the *original* source. // prefab variant instances will show up here as nestedprefab instances // if the variants are in variants. :P goPrefabObj = PrefabUtility.GetCorrespondingObjectFromOriginalSource(go); } else { // PrefabVariantInstance means we need the corresponding source. goPrefabObj = PrefabUtility.GetCorrespondingObjectFromSource(go); } GameObject root = go; #else goPrefabObj = (GameObject)PrefabUtility.GetPrefabParent(go); GameObject root = PrefabUtility.FindPrefabRoot(go); #endif bool isRootOfPrefab = root == go; if (isRootOfPrefab && goPrefabObj == objID.obj) { //Instance found! // Debug.Log("[SearchItemInstances] instance found!"+go.name); SearchResult result = new SearchResult(); result.strRep = ""; result.pathInfo = PathInfo.GetPathInfo(go, job.assetData); result.actionTaken = SearchAction.InstanceFound; job.MatchFound(result, this); replaceItem.ReplaceInstance(job, this, go, result); } } }
public void DrawValidSearch(bool isValid, UnityEngine.Object obj) { if (!isValid) { string warningInfo = ""; if (obj != null) { PrefabTypes type = PrefabUtil.GetPrefabType(obj); if (type == PrefabTypes.PrefabInstance || type == PrefabTypes.NestedPrefabInstance) { warningInfo = obj.name + " is a prefab instance, not a prefab."; } else { warningInfo = obj.name + " is not a prefab root."; } } if (warningInfo.Length > 0) { EditorGUILayout.HelpBox(warningInfo, MessageType.Warning); } } }
public static bool ValidateAndAssign(UnityEngine.Object newObj, ObjectID objID, PropertyPopupData searchProperty, ref InitializationContext initializationContext) { // First we need to attempt validation, or find the 'real' object we are // interested in. UnityEngine.Object contextObj = newObj; bool objectValid = true; // If the object is a 'scene' object, let's get the prefab object if it // exists. if (newObj != null) { if (newObj.GetInstanceID() < 0) { // Debug.Log("[SearchItemProperty] finding prefab."); #if UNITY_2018_2_OR_NEWER UnityEngine.Object prefab = PrefabUtility.GetCorrespondingObjectFromSource(newObj); #else UnityEngine.Object prefab = PrefabUtility.GetPrefabParent(newObj); #endif if (prefab != null) { newObj = prefab; } } PrefabTypes newObjPrefabType = PrefabUtil.GetPrefabType(newObj); if (newObjPrefabType == PrefabTypes.NotAPrefab) { if (newObj is Component) { if (newObj is MonoBehaviour) { MonoScript script = MonoScript.FromMonoBehaviour((MonoBehaviour)newObj); if (script != null) { newObj = script; } } } } } if (newObj is MonoScript) { MonoScript m = (MonoScript)newObj; if (typeof(MonoBehaviour).IsAssignableFrom(m.GetClass()) || typeof(ScriptableObject).IsAssignableFrom(m.GetClass())) { // Debug.Log("[SearchItemProperty] Valid monobehaviour"+m.GetClass()); } else { Debug.LogWarning("[Search And Replace] The object given does not contain a script."); objectValid = false; } } //Ok, validation is complete, let's process the new object. if (objectValid) { objID.SetObject(newObj); searchProperty.SetType(objID.obj); initializationContext = new InitializationContext(searchProperty.fieldData, contextObj); initializationContext.forceUpdate = true; SRWindow.Instance.PersistCurrentSearch(); return(true); } return(false); }
public GameObject getPrefabByType(PrefabTypes type) { return(prefabs[(int)type]); }
void OnGUI() { //Use PreExisting Prefab /* if(isEdit) { newTitle = newUnit.title; newName = newUnit.title; // newHealth = newUnit.health; newSpeed = newUnit.speed; newDefenseType = newUnit.defenseType; newDefenseRating = newUnit.defenseRating; newSprite = newUnit.sprite; newAnimator = newUnit.animator; isEdit = false; } */ prefabType = (PrefabTypes)EditorGUILayout.EnumPopup (prefabType); EditorGUILayout.Space (); switch (prefabType) { case PrefabTypes.Tower: type = "Tower"; isUnit = false; /*hasSecondAtk = true; atk2IsActive = false; canMine = true; mineIsActive = true;*/ break; case PrefabTypes.Unit: EditorGUILayout.BeginHorizontal (); unitType = (UnitTypes)EditorGUILayout.EnumPopup (unitType); type = unitType.ToString (); EditorGUILayout.Space (); EditorGUILayout.EndHorizontal (); EditorGUILayout.Space (); isUnit = true; /*hasSecondAtk = false; atk2IsActive = false; canMine = false; mineIsActive = false;*/ break; } EditorGUILayout.BeginHorizontal (); EditorGUI.indentLevel = 0; EditorGUILayout.LabelField ("Title: ", GUILayout.Width (35)); newTitle = EditorGUILayout.TextField (newTitle, GUILayout.Width (200), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField ("Unique:", GUILayout.Width (50), GUILayout.ExpandWidth(false)); isUnique = EditorGUILayout.Toggle(isUnique, GUILayout.Width (10), GUILayout.ExpandWidth(false)); EditorGUILayout.Space (); EditorGUILayout.EndHorizontal (); EditorGUILayout.Space (); if (isUnique) { EditorGUILayout.BeginHorizontal (); EditorGUI.indentLevel = 1; EditorGUILayout.LabelField ("Name: ", GUILayout.Width (60)); newName = EditorGUILayout.TextField (newName,GUILayout.Width (200), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal (); EditorGUI.indentLevel = 0; } else { newName = newTitle; } EditorGUILayout.Space (); newSprite = (Sprite)EditorGUILayout.ObjectField ("Sprite: ", newSprite, typeof(Sprite), true); EditorGUILayout.Space (); //Animator??? newAnimator = (Animator)EditorGUILayout.ObjectField ("Animator: ", newAnimator, typeof(Animator), true); EditorGUILayout.Space (); EditorGUILayout.BeginHorizontal (); EditorGUI.indentLevel = 1; EditorGUILayout.LabelField ("Health: ", GUILayout.Width (80)); newHealth = EditorGUILayout.IntField (newHealth); EditorGUILayout.Space (); if (isUnit) { EditorGUILayout.LabelField ("Speed:", GUILayout.Width (80)); newSpeed = EditorGUILayout.FloatField (newSpeed); EditorGUILayout.Space (); } EditorGUILayout.EndHorizontal (); EditorGUILayout.Space (); EditorGUILayout.BeginHorizontal (); EditorGUI.indentLevel = 1; EditorGUILayout.LabelField ("Defense: ", GUILayout.Width (80)); newDefenseType = (Defenses)EditorGUILayout.EnumPopup (newDefenseType, GUILayout.Width (100)); //EditorGUILayout.Space (); EditorGUILayout.LabelField ("Rating:", GUILayout.Width (60)); newDefenseRating = EditorGUILayout.FloatField (newDefenseRating, GUILayout.Width (40)); EditorGUILayout.Space (); EditorGUILayout.EndHorizontal (); EditorGUILayout.Space (); EditorGUILayout.Space (); EditorGUILayout.BeginHorizontal (); EditorGUI.indentLevel = 0; EditorGUILayout.LabelField ("Attack :", GUILayout.Width (150)); EditorGUILayout.Space (); atk1IsActive = EditorGUILayout.Toggle (atk1IsActive, GUILayout.Width (30)); EditorGUILayout.EndHorizontal (); EditorGUILayout.BeginHorizontal (); EditorGUI.indentLevel = 1; EditorGUILayout.LabelField ("Object: ", GUILayout.Width (80)); newAttack1Projectile = (GameObject)EditorGUILayout.ObjectField (newAttack1Projectile, typeof(GameObject), true);//, GUILayout.Width(100)); EditorGUILayout.LabelField ("Damage: ", GUILayout.Width(80)); atk1Damage = EditorGUILayout.FloatField (atk1Damage); EditorGUILayout.EndHorizontal (); atk1Type = (AttackTypes)EditorGUILayout.EnumPopup("Type: ", atk1Type); EditorGUILayout.BeginHorizontal (); EditorGUILayout.LabelField ("Range: ", GUILayout.Width (80)); atk1Range = EditorGUILayout.FloatField (atk1Range); EditorGUILayout.LabelField ("CoolDown: ",GUILayout.Width (80)); atk1CD = EditorGUILayout.FloatField (atk1CD); EditorGUILayout.EndHorizontal (); EditorGUILayout.BeginHorizontal (); EditorGUILayout.LabelField ("Accuracy: ", GUILayout.Width (80)); atk1Accuracy = EditorGUILayout.FloatField (atk1Accuracy); EditorGUILayout.LabelField ("Speed: ", GUILayout.Width (80)); atk1Speed = EditorGUILayout.FloatField (atk1Speed); EditorGUILayout.EndHorizontal (); EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal (); EditorGUILayout.Space (); hasSecondAtk = EditorGUILayout.Toggle ("Has Second Attack: ", hasSecondAtk); EditorGUILayout.EndHorizontal (); EditorGUILayout.Space (); EditorGUILayout.Space (); if(hasSecondAtk) { EditorGUILayout.BeginHorizontal (); EditorGUI.indentLevel = 0; EditorGUILayout.LabelField ("Attack 2 :", GUILayout.Width (150)); EditorGUILayout.Space (); atk2IsActive = EditorGUILayout.Toggle (atk2IsActive, GUILayout.Width (30)); EditorGUILayout.EndHorizontal (); EditorGUILayout.BeginHorizontal (); EditorGUI.indentLevel = 1; EditorGUILayout.LabelField ("Object: ", GUILayout.Width (80)); newAttack2Projectile = (GameObject)EditorGUILayout.ObjectField (newAttack1Projectile, typeof(GameObject), true); EditorGUILayout.LabelField ("Damage: ", GUILayout.Width(80)); atk2Damage = EditorGUILayout.FloatField (atk1Damage); EditorGUILayout.EndHorizontal (); atk2Type = (AttackTypes)EditorGUILayout.EnumPopup ("Type: ", atk2Type); EditorGUILayout.BeginHorizontal (); EditorGUILayout.LabelField ("Range: ", GUILayout.Width(80)); atk2Range = EditorGUILayout.FloatField (atk2Range); EditorGUILayout.LabelField ("CoolDown: ", GUILayout.Width (80)); atk2CD = EditorGUILayout.FloatField (atk2CD); EditorGUILayout.EndHorizontal (); EditorGUILayout.BeginHorizontal (); EditorGUILayout.LabelField ("Accuracy: ", GUILayout.Width (80)); atk2Accuracy = EditorGUILayout.FloatField (atk2Accuracy); EditorGUILayout.LabelField ("Speed: ", GUILayout.Width (80)); atk2Speed = EditorGUILayout.FloatField (atk2Speed); EditorGUILayout.EndHorizontal (); atk2OnGC = EditorGUILayout.Toggle ("On GC: ", atk2OnGC); } EditorGUI.indentLevel = 1; EditorGUILayout.Space (); EditorGUILayout.BeginHorizontal (); EditorGUILayout.Space (); canMine = EditorGUILayout.Toggle ("Can Mine: ", canMine); EditorGUILayout.EndHorizontal (); EditorGUILayout.Space (); if(canMine) { ///Needs to Include: /// Mining Range, Mining Cooldown, Mining Efficiency x4 /// EditorGUILayout.BeginHorizontal (); EditorGUI.indentLevel= 0; EditorGUILayout.LabelField ("Mining: ", GUILayout.Width (150)); EditorGUILayout.Space (); mineIsActive = EditorGUILayout.Toggle (mineIsActive, GUILayout.Width (30)); EditorGUILayout.EndHorizontal (); EditorGUILayout.BeginHorizontal (); EditorGUI.indentLevel = 1; EditorGUILayout.LabelField ("Range: ", GUILayout.Width (80)); mineRange = EditorGUILayout.FloatField (mineRange); EditorGUILayout.LabelField ("CoolDown: ", GUILayout.Width (80)); mineCD = EditorGUILayout.FloatField (mineCD); EditorGUILayout.EndHorizontal (); EditorGUILayout.BeginHorizontal (); EditorGUI.indentLevel = 2; EditorGUILayout.LabelField ("Iron: ", GUILayout.Width (80)); mineEffIron = EditorGUILayout.FloatField (mineEffIron); EditorGUILayout.LabelField ("Lode: ", GUILayout.Width (80)); mineEffLode = EditorGUILayout.FloatField (mineEffLode); EditorGUILayout.Space (); EditorGUILayout.EndHorizontal (); EditorGUILayout.BeginHorizontal (); EditorGUILayout.LabelField ("Brin: ", GUILayout.Width (80)); mineEffBrin = EditorGUILayout.FloatField (mineEffBrin); EditorGUILayout.LabelField ("Mithril: ", GUILayout.Width (80)); mineEffMith = EditorGUILayout.FloatField (mineEffMith); EditorGUILayout.Space (); EditorGUILayout.EndHorizontal (); } EditorGUILayout.Space (); if(GUILayout.Button ("Register " + type)) { //Build a New Prefab GameObject newUnit = (GameObject.Find ("UnitPrefab")); newUnit.name = newName; string folder = "Tower"; if(isUnit) { folder = "Units"; } string path = AssetDatabase.GenerateUniqueAssetPath ("Assets/Prefabs/" + folder + "/" + newUnit.name + ".prefab"); PrefabUtility.CreatePrefab (path, newUnit); //if(size == medium) var capsule = newUnit.GetComponent<CapsuleCollider>(); capsule.radius = 1; capsule.height = 4; var body = newUnit.AddComponent<BodyScript>(); body.title = newTitle; //body.speed = newSpeed; body.maxHealth = newHealth; body.health = newHealth; body.defenseRating = newDefenseRating; body.defenseType = newDefenseType.ToString (); if(isUnit) { //var seeker = newUnit.AddComponent<Seeker>(); var controller = newUnit.AddComponent<CharacterController>(); controller.radius = 0; controller.height = 0; var walker = newUnit.AddComponent<WalkScript>(); walker.baseSpeed = newSpeed; var funnelMod = newUnit.AddComponent<Pathfinding.FunnelModifier>(); //funnelMod.priority = 2; var alternMod = newUnit.AddComponent<Pathfinding.AlternativePath>(); //alternMod.priority = 3; } var attack = newUnit.AddComponent<AttackScript>(); attack.attackIsActive = atk1IsActive; attack.attackProjectile = newAttack1Projectile; attack.attackDamage = atk1Damage; attack.attackType = atk1Type; attack.attackRange = atk1Range; attack.attackCooldown = atk1CD; attack.attackAccuracy = atk1Accuracy; attack.attackSpeed = atk1Speed; if(hasSecondAtk) { attack.attack2IsActive = atk2IsActive; attack.attack2Projectile = newAttack2Projectile; attack.attack2Damage = atk2Damage; attack.attack2Type = atk2Type; attack.attack2Range = atk2Range; attack.attack2Cooldown = atk2CD; attack.attack2Accuracy = atk2Accuracy; attack.attack2Speed = atk2Speed; attack.attack2OnGC = atk2OnGC; } if(canMine) { var mine = newUnit.AddComponent<MiningScript>(); mine.miningRange = mineRange; mine.miningCooldown = mineCD; mine.ironMiningRate = mineEffIron; mine.lodestoneMiningRate = mineEffLode; mine.brinmistrMiningRate = mineEffBrin; mine.mithrilMiningRate = mineEffMith; } } }
public override void Draw(SearchOptions options) { GUILayout.BeginHorizontal(SRWindow.searchBox); // 1 GUILayout.BeginVertical(); drawSubsearch(); GUILayout.BeginHorizontal(); Rect r = EditorGUILayout.BeginHorizontal(); Event e = Event.current; bool acceptingDrag = (e.type == EventType.DragUpdated || e.type == EventType.DragPerform) && r.Contains(e.mousePosition); if (acceptingDrag) { if (DragAndDrop.objectReferences.Length == 1) { UnityEngine.Object firstObj = DragAndDrop.objectReferences[0]; SRWindow.Instance.Repaint(); DragAndDrop.AcceptDrag(); DragAndDrop.visualMode = DragAndDropVisualMode.Link; if (e.type == EventType.DragPerform) { draggedObj = firstObj; } } else { DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; } } string dragText = null; if (objID.obj != null) { if (objID.obj is MonoScript) { MonoScript m = (MonoScript)objID.obj; dragText = "Currently searching " + m.GetClass().Name + "s."; } else { dragText = "Currently searching " + objID.obj.GetType().Name + "s."; } dragText += "\n(Drag an object here to change search)"; } else { dragText = "Drag an object here."; } if (r.Contains(e.mousePosition) && DragAndDrop.visualMode == DragAndDropVisualMode.Link) { GUILayout.BeginVertical(SRWindow.searchBoxDragHighlight); GUILayout.Label(new GUIContent(dragText), SRWindow.richTextStyle); GUILayout.EndVertical(); } else { GUILayout.BeginVertical(SRWindow.searchBox); GUILayout.Label(new GUIContent(dragText), SRWindow.richTextStyle); GUILayout.EndVertical(); } EditorGUILayout.EndHorizontal(); if (propertyCriteria.hasAdvancedOptions()) { bool newShowMoreOptions = EditorGUILayout.Toggle(showMoreOptions, SRWindow.optionsToggle, GUILayout.Width(15)); if (newShowMoreOptions != showMoreOptions) { showMoreOptions = newShowMoreOptions; propertyCriteria.showMoreOptions = showMoreOptions; SRWindow.Instance.PersistCurrentSearch(); } } GUILayout.EndHorizontal(); if (objID.obj != null) { string typeInfo = ""; if (searchProperty.HasOptions()) { typeInfo = "Searching the " + searchProperty.fieldData.objectType.Name + " property <b>" + searchProperty.fieldData.fieldName + propertyCriteria.StringValueWithConditional() + "</b>"; } string warningInfo = ""; PrefabTypes pType = PrefabUtil.GetPrefabType(objID.obj); if (pType == PrefabTypes.NotAPrefab) { if (objID.obj is GameObject || objID.obj is Component) { warningInfo = "Referencing a scene component, your search will not be saved on scene change."; } else if (SRWindow.Instance.isSearchingInScene() && !SRWindow.Instance.isSearchingDependencies()) { warningInfo = "Searching in a scene but this is not a scene object. No results will be found."; } } string ssw = subsearchWarning(); if (ssw != string.Empty) { warningInfo += " " + ssw; } if (warningInfo.Length > 0) { EditorGUILayout.HelpBox(warningInfo, MessageType.Warning); } if (typeInfo.Length > 0) { EditorGUILayout.LabelField(typeInfo, SRWindow.richTextStyle); } searchProperty.Draw(); if (searchProperty.HasOptions()) { initializationContext.updateFieldData(searchProperty.fieldData); propertyCriteria.SetType(initializationContext); initializationContext.forceUpdate = false; SearchOptions typeFieldOptions = options.Copy(); if (subsearch != null) { typeFieldOptions.searchType = SearchType.Search; } propertyCriteria.Draw(typeFieldOptions); } } if (subsearch != null) { SearchItem child = (SearchItem)subsearch; child.Draw(options); } drawAddRemoveButtons(); GUILayout.EndVertical(); GUILayout.EndHorizontal(); // 1 if (e.type == EventType.DragExited && draggedObj != null && objID.obj != draggedObj) { ObjectUtil.ValidateAndAssign(draggedObj, objID, searchProperty, ref initializationContext); draggedObj = null; propertyCriteria.SetType(initializationContext); } }
public PrefabComponent(PrefabTypes type, Entity prefab) { this.type = (int)type; this.prefab = prefab; }