Yes, I have read all the topics with similar problems.
However mine is not solved yet :(
So could you please take a look into it?
Like everyone else, I'm trying to change a sprite of an object via code.
The sprite file itself is in "Assets/Resourses" folder.
The import settings state the texture type of the imported file is "Sprite(2D and UI)".
I've tried the following methods:
gameObject.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("1");
and
gameObject.GetComponent<SpriteRenderer>().sprite = Resources.Load("1") as Sprite;
and
Sprite s = Resources.Load("1") as Sprite;
gameObject.GetComponent<SpriteRenderer>().sprite = s;
and
Sprite s = Resources.Load<Sprite>("1");
gameObject.GetComponent<SpriteRenderer>().sprite = s;
All of them replace existing object sprite with "None(Sprite)", which I guess, means "null".
Any help would be really appreciated!
The sprite file itself is in "Assets/Resourses" folder.
That's the problem. The folder must be named Resources not Resourses. It's not Resource too.
It has to be named named Resources. Once you fix the folder name, you can use the snippet below to read the Sprite.
Sprite sprite = Resources.Load("1", typeof(Sprite)) as Sprite;
if the sprite is set to multiple mode then use this;
Sprite[] sprite = Resources.LoadAll<Sprite>("1") as Sprite[];
You can find other ways to load Sprites here.
Related
Alright, working in Unity 2019.4.1 I have a glitch where, if I change the font of my Text Mesh via script at runtime, the characters get garbled:
I tried a solution from the forums here:
void resetChars(TextMesh textMesh)
{
textMesh.text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
textMesh.font.characterInfo = null;
textMesh.font.RequestCharactersInTexture(textMesh.text, textMesh.fontSize, textMesh.fontStyle);
}
if(Input.GetKey("space"))
{
resetChars(GetComponent<TextMesh>());
GetComponent<TextMesh>().font = (Font)Resources.Load("Inter-Black-slnt=0");
}
But this made it worse. What is happening here and how can I change my Text Mesh font dynamically?
Put your font in a Resources file directly in asset file then use that method style.font = Resources.Load<Font>("Fonts/<fileName");.
https://docs.unity3d.com/2018.1/Documentation/ScriptReference/UI.Text-font.html look here to.
The material of the MeshRenderer must be the same as the font's material.
I want to change AudioSources volume and I want to achieve it by changing AudioMixerGroup.
I am trying to change AudioSources's AudioMixerGroup to a new one via script and I am loading it the following way:
audioSource.outputAudioMixerGroup = Resources.Load("Resources/AudioMixerWithSound") ; //Here it can convert an object to explicit AudioMixerGroup.
//audioSource.AudioMixerGroup = Resources.Load("AudioMixer/AudioMixerGroup") as AudioMixerGroup; // Here AudioMixerGroup doesn't exist.
So how can I change the AudioSOurce's outputAudioMixeGroup?
There is really no Unity Asset named AudioMixerGroup. Notice that the only audio mixing asset you can create is AudioMixer if you go to Assets --> Create --> AudioMixer. If this is how you created the mixer then the resources file type to load is AudioMixer not AudioMixerGroup and the extension should be ".mixer".
Note that you don't include the resources folder name in the Resources.Load function. If the "AudioMixerWithSound" file to load is the Resources folder, you would use Resources.Load("AudioMixerWithSound") to load it instead of Resources.Load("Resources/AudioMixerWithSound"). Also, the extension ".mixer" is not included.
Loading the AudioMixer file from the Resources folder:
//Get the AudioSource
AudioSource audioSource = GetComponent<AudioSource>();
//Load AudioMixer
AudioMixer audioMixer = Resources.Load<AudioMixer>("AudioMixerWithSound");
//Find AudioMixerGroup you want to load
AudioMixerGroup[] audioMixGroup = audioMixer.FindMatchingGroups("Master");
//Assign the AudioMixerGroup to AudioSource (Use first index)
audioSource.outputAudioMixerGroup = audioMixGroup[0];
Note that where the AudioMixerGroup is found with FindMatchingGroups("Master"), if it is a child object, you can use / to access the child like you would with the GameObject.Find function. For example, FindMatchingGroups("Master/child"). See the doc for more info.
I am trying to get a video (.ogv) from the Streaming Assets folder, apply it on to a surface and play it. (C# code added below) - Unity 5 Personal
void Start () {
private MovieTexture myMoviePlayerTexture;
string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "test.ogv");
WWW www = new WWW("file:///" + filePath);
Renderer r = GetComponent<Renderer>();
r.material.mainTexture = www.movie;
www.movie.Play();
}
C# Script added to a simple plane.
However when I add the MovieTexture to a plane (from assets folder) it plays without any problems.
((MovieTexture)GetComponent<Renderer>().material.mainTexture).Play();
edit: the Problem: the video does not play from the Streaming Assets folder, it appears as if a material is loaded but it does not play.
Any help appriciated. Thanks.
EDIT
This is the game view: link and this is the folder structure link nothing fancy just some test videos
The code in your question is not even compiling. There are so many errors.
Anyways, don't play the video directly from the WWW object with www.movie.Play();. Get the MovieTexture from WWW first then play the video from there. Also, check if MovieTexture.isReadyToPlay is true before playing the video. You can do this in a coroutine or the Update() function.
Make sure that the test.ogv video is in the StreamingAssets folder. Your screenshot does not show what's inside the StreamingAssets folder.
Finally, playing video on a plane is now old. You can play Video on Unity's RawImage which is easier to work with in Unity.
I can't tell which Unity 5 version you are using but please update to 5.5 or above then use the code below:
MovieTexture myMoviePlayerTexture;
void Start()
{
string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "test.ogv");
WWW www = new WWW("file:///" + filePath);
Renderer r = GetComponent<Renderer>();
myMoviePlayerTexture = www.GetMovieTexture();
r.material.mainTexture = myMoviePlayerTexture;
}
void Update()
{
if (myMoviePlayerTexture.isReadyToPlay && !myMoviePlayerTexture.isPlaying)
{
myMoviePlayerTexture.Play();
}
}
It's worth noting that MovieTexture is now deprecated. See this post for the newsiest method to play video.
I want to change the image that clicked image button.
so, I set a button and try to load a PNG file at assets/Resources/ PATH.
But it always return and it makes me massed up -_-.
Some of questions likes my situation, everybody answers 'try to move file to load assets/Resoucres PATH and my situation couldn't be fixed that way.
Here is my code. This function is load a NumCard_1.png file when clicked a image button. NumCard_1.png exists at assets/Resources folder.
void OnclkMe()
{
Sprite newSprite = Resources.Load<Sprite>("/NumCard_1") as Sprite;
// I already try ("NumCard_1") instead above.
if(newSprite==null)
{
Debug.Log("NULL"); // Always prints "NULL" -_-.
}
else
{
Debug.Log(newSprite.name);
}
}
When using Resources.Load in Unity, it uses a relative folder to "Resources"
every resource you intend to use at runtime must be located under /Resources, if it ain't there - create it.
after you create this base folder (under Assets) you can create subfolders by your own preference.
secondly, Resources.Load("/NumCard_1") as Sprite is kind of misused.
the generic method Resources.Load returns T so you can drop the "as Sprite" (it will act the same).
as Unity uses "Resources" as base folder you should remove the "/" before "NumCard".
if you use subfolder you might want to specify the path as "Cards/NumCard_1".
as per your code, after creating the folder Assets/Resources and placing the img in it, try the following:
Sprite newSprite = Resources.Load<Sprite>("NumCard_1")
Add your sprite in resources folder.
Change texture type to Sprite, and apply.
Sprite newSprite = Resources.Load("NumCard_1",typeof(Sprite)) as Sprite;
Maybe you don't set import config of that file into sprite? Normally png would be imported as texture, not sprite
First you should try to check it like this
var obj = Resources.Load("NumCard_1");
Debug.Log(typeof(obj));
If obj is not null, it would tell you what type it is. If it not sprite you would need to go to unity editor, select file, and change import type that png into sprite
ps Using generic mean you can just write is like this
var obj = Resources.Load<Sprite>("NumCard_1");
And obj will be sprite. If it not a sprite it would be null
Folder Name Must be named "Resourses" in asset
I'm making a C# game using Visual C# Studio 2010 Express with Microsoft's XNA.
At the moment I'm trying to load content into the game but I'm having trouble with the relative Content path. My current code looks like this:
private Texture2D planetBackground;
private Texture2D groundFacility;
private Texture2D hoverShip;
private Texture2D attackShip;
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
planetBackground = Content.Load<Texture2D>("spacebackground.png");
groundFacility = Content.Load<Texture2D>("planetstation.png");
hoverShip = Content.Load<Texture2D>("ship2.png");
attackShip = Content.Load<Texture2D>("ship1.png");
}
The Content.RootDirectory is currently set to "Content".
How do I construct the paths to the images so that they will load? At the moment I get a ContentLoadException: file not found, so clearly my relative path is wrong. Where does the path start from?
Remove file extensions:
planetBackground = Content.Load<Texture2D>("spacebackground");
XNA transforms all files into .XNB files, so there's no need to specify extensions (unless your files have multiple dots in file names).