i am trying to automate the process of manual selection of game object from hierarchy then drag into the project panel by mouse then select Track dependencies from the menu and exporting the prefab into .Unity3D file
i want to automate this by code .
i think of the process like ... Create prefab then buildpipeline.BuildAssetBundle ,,,, but the first 2 parameters in buildpipeline.BuildAssetBundle are the Problem i can't make them fit with a gameobject nor prefab
the first 1 takes (object ) as main asset and the second takes (object[]) which refer to the inside asset files ..... but i can't put this to refer to a gameobject nor prefab .
GameObject maw;
string paths="Assets";
public PrefabType dude;
void Start () {
dude=PrefabUtility.CreatePrefab("Assets\temp",maw);
// the problem is here the first 2 parameters ......
BuildPipeline.BuildAssetBundle((Object) dude, (Object[])dude, "Assets",
BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets , BuildTarget.Android);
}
Related
Desc:
There is a component with list data:
public class TestPrefab : MonoBehaviour
{
public List<string> DataList;
}
Then I create a prefab with this component like this:
Then I drag the prefab into scene, modify the component data like this ("Data1"=>"Data1_Modified") (I wont apply changes because I just wanna keep it as a prefab instance, which has some changes from the origin prefab):
Then back to the prefab stage, delete the first data:
Then the prefab instance in the scene comes to:
How does it happen?
unity save this modification like this:
so unity just change the first data in the list to "Data1_Modified", rather than change "Data1" to "Data1_Modified".
Question:
Is there any elegant method to avoid this unpredicted behavior?
you should apply this edits on prefabs so after you finish in inspector menu press on overrides then apply all
prefabs are prefabricated objects so to update them from Scene window you need to tell editor to apply your changes on prefabs.
I am building an AR app using AR Foundation but got stuck with changing the material of my model during runtime by pressing a button.
The script below works perfectly fine when I attach it to my model and then create buttons with OnClick() Events to access the specific material WHEN my model is in the hierarchy.
But I don't want it to stay in the hierarchy because otherwise it will constantly show up in AR. If I remove it from the hierarchy, the OnClick() is obviously missing a prefab.
So I need to have the possibility to still change materials from button clicks but without my model staying in the hierarchy. I think "AddListener" could be a solution, but I don't really know how to edit the script in order to make it work. With AddListener I wouldn't have to use OnClick().
Can anyone please show me the solution?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColor : MonoBehaviour
{
public Material[] material;
Material setMaterial;
Renderer rend;
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled=true;
}
void Update()
{
}
public void GreyMaterial()
{
rend.sharedMaterial = material[0];
setMaterial = rend.material;
}
public void YellowMaterial()
{
rend.sharedMaterial = material[1];
setMaterial = rend.material;
}
public void RedMaterial()
{
rend.sharedMaterial = material[2];
setMaterial = rend.material;
}
}
You can out your prefab in folder called "Resources". And get your prefab using Resources.Load in script next way:
GameObject prefab = Resources.Load<GameObject>("Path"); // Path WITHOUT "Resources/"
Here prefab is... your prefab and you get change some components and values in it.
P.S.
It is very important to write Resources letter to letter without mistakes
You may put folders into (folders into folders...) into Resources, but here you must set the exact path to your file.
I believe the script is not referencing all the new instantiated objects, its needs to reference them. Either store those new objects in a list that the script can change, or try to search for every item of that material you want to change when you run the change material function(not very efficient, but might be easier)
i found this on youtube
I have a player prefab that is spawned when the game starts via Network Manager, the player prefab has a script(Player Health) attached to it which takes 2 UI element as input(DamageImage, HealthSlider) present in the hierarchy.The problem i face is that I cant add these inputs in the prefab which results in Object reference not set to an instance of an object error.
Tried to add them by placing the prefab in hierarchy and then saving it but it did not help.
If the UI elements aren't in the prefab itself or in one of its children then you will lose the reference once you make the prefab.
What you can do is set the tag of those elements to something like "DamageImage" and "HealthSlider" and then in the Awake() function of a your PlayerHealth script attached to your prefab do this:
void Awake(){
DamageImage = GameObject.FindWithTag("DamageImage").GetComponent<Image>();
HealthSlider = GameObject.FindWithTag("HealthSlider").GetComponent<Slider>();
}
Okay so I've been making a 2d platformer and had a terrible image/sprite for my player. Now I've got a better one and want to just replace the images but keep all the same values and data/scripts etc.
I've been trying to figure it out for awhile but to no avail. Thanks for any help
If you want to permanently change a sprite on your prefabs/objects you can drag the new sprite from your asset folder to into the "Sprite Renderer" Component of the object you want to change, replacing whatever is currently in the sprite box. Check out this image to see exactly where you want to drag the sprite:
O you can change it via script using a public variable:
public class ChangeSprite: MonoBehaviour
{
public Sprite newSprite;
private void ChangeSprite(){
gameObject.GetComponent<SpriteRenderer>().sprite == newSprite;
}
}
One way to do it, not sure it is the best way but it works, is to:
- import your new Sprite (let's call it SpriteB)
- select the GameObject where you have been using your first sprite (SpriteA)
- in the "Sprite Renderer" component of your selected GameObject, replace "Sprite=SpriteA" with your new sprite so that "Sprite=SpriteB"
Obviously you will have to repeat the operation for every GameObject where you may have used SpriteA.
A bit new to Unity, I know that you can get a prefab from the instance of the GameObject (right click on GameObject and Select Prefab). But is there a way to select the GameObjects and Components that were used to created a Prefab? (from the Prefab)
Try this example out. It contains two flavors of selection, one to select all components attached to the prefab, and the other to select just the monobehaviours attached to the prefab.
What this script will do is check what components / monobehaviours are attached to the currently selected GameObject (or prefab) and will output them to the log, as well as select them all.
Copy the below code, put it into a new script named "PrefabComponentSelectorEditor" and save it in a folder called "Editor".
You should then see a Menu Item called "AxS -> Prefab Component Selector". Click on it, and see the console as well as the inspector.
The code is commented, but in case you have any questions, feel free to ask.
P.S. Remember to use ONLY ONE of the flavours (i.e. Monobehvaiour or Component). Comment / Uncomment the code accordingly. I have left the component version uncommented, and the monobehaviour version is right below it.
using UnityEngine;
using UnityEditor;
using System.Collections;
public class PrefabComponentSelectorEditor : EditorWindow {
[MenuItem("AxS/Prefab Component Selector")]
public static void SelectComponents () {
Debug.Log ("GOs "+Selection.activeGameObject);
if(Selection.activeGameObject != null) {
//This will select all the COMPONENTS attached to the prefab
//Let's first get all the components this GameObject has
Component[] components = Selection.activeGameObject.GetComponents<Component>() as Component[];
//If the length of components is > 0 (always WILL be, since every GameObject is
//guarenteed to have a Transform Component at the very minimum
if(components.Length > 0) {
//Print all the components to console
foreach(Component component in components)
Debug.Log (component);
//Set the current selection to the components
Selection.objects = (Object[])components;
}
//This will select all the MONOBEHAVIOURS attached to the prefab
//First get all Monobehavious attached
//MonoBehaviour[] behaviours = Selection.activeGameObject.GetComponents<MonoBehaviour>() as MonoBehaviour[];
//Check if the length of the array is > 0. This is required, since a GameObject
//may have no scripts attached to it
//if(behaviours.Length > 0) {
//Print all attached Monobehvaiours to console
// foreach(MonoBehaviour behaviour in behaviours)
// Debug.Log (behaviour);
//Set the current selection to the Monobehaviours
// Selection.objects = (Object[])behaviours;
//}
//The difference between COMPONENT and MONOBEHAVIOUR
//A Monobehaviour is basically any script you make, so if you use the monobehaviour version
//it will only select the scripts you attach to the prefab
//A component is anything you can see on the inspector, including Unity's inbuilt components
//Essentially, all Monobehaviours are Components
//Eg. If your prefab has a single script called "ExampleScript" attached to it, and has a BoxCollider
//Monobehaviour version will output just the ExampleScript
//Component version will output the Transform, BoxCollider and ExampleScript
}
}
}