Load sprite from a psb file using script - c#

I saved the path of the sprite I used in a json file:
AssetDatabase.GetAssetPath(weapon.weaponSprite);
output:
{"weaponSprite": "Assets/Prefabs/Equipment/PSBs/Axe_Set.psb"}
then I tried to load that sprite: Resources.Load<Sprite>(myData.weaponSprite) however that only loads the psb file, inside the psb file is the sprite that I needed. It has a name Axe_001. How do I load this?

Related

How can I "ping" a text file in a custom Unity3D Inspector?

I'm trying to "ping" a Textfile in a custom editor in Unity3D like e.g. using EditorGUIUtility.PingObject (Shows the file in the Hierachy and flashes a yellow selection field over it).
The file is under Assets/StreamingAssets/Example.csv
The simplest solution (I thought) would be to simply show it in an ObjectField -> if clicking on the field the asset also gets "pinged".
so I'm trying:
// For debug, later the filename will be dynamic
var path = "Assets/StreamingAssets/" + "Example" + ".csv";
TextAsset file = (TextAsset)AssetDatabase.LoadAssetAtPath(path, typeof(TextAsset));
EditorGUILayout.PrefixLabel("CSV File", EditorStyles.boldLabel);
EditorGUILayout.ObjectField(file, typeof(TextAsset), false);
But though the file is there and the path correct, file is allways null
Unfortunately, your code is fine and this is a bug.
The bug occurs when the asset is placed in the StreamingAssets folder that is in the Assets folder. This causes the AssetDatabase.LoadAssetAtPath function to fail. I did search about this and only one post came up without any workaround. I mean you can use one of the API from the System.IO namespace to read the file but then you won't have access to the Object that references the file.
Possible Fix:
1.Restart Unity.
2.Create a folder named "Test" in the Assets folder.
3.Drag the StreamingAssets folder to this "Test" folder.
4.Change the path in the code to var path = "Assets/Test/StreamingAssets/" + "Example" + ".csv"; then click "Play". It should not be null. If it's no longer null, move the StreamingAssets folder back to the Assets folder and also change the path in code to the old path.
The steps above is how I fixed it on my side and it works now. If that doesn't work, I suggest you move the "Example.csv" file to the Assets folder then use var path = "Assets/" + "Example" + ".csv"; to read it. If it works, move it back to the StreamingAssets folder and change the path to the old path.
Another thing I suggest you do is call AssetDatabase.Refresh() to refresh the project.
I also suggest you file for bug report for this issue.
My mistake was the typecasting to TextAsset and using LoadAssetAtPath which requires a Type parameter.
Leaving it "uncasted" as object (asset) and using LoadMainAssetAtPath instead which doesn't require the Type parameter works now:
// For debug, later the filename will be dynamic
var path = "Assets/StreamingAssets/" + "Example" + ".csv";
var file = AssetDatabase.LoadMainAssetAtPath(path);
EditorGUILayout.PrefixLabel("CSV File", EditorStyles.boldLabel);
EditorGUILayout.ObjectField(file, typeof(object), false);

What is a .pex file (in related to Urho2d sample)?

This is what I read in:
https://github.com/xamarin/urho-samples/blob/master/FeatureSamples/Core/25_Urho2DParticles/Urho2DParticle.cs[Urho2D][1]
Line 90:
ParticleEffect2D particleEffect = cache.GetParticleEffect2D("Urho2D/sun.pex");
Does anyone have a clue on how to produce one of these particle effect and put it into a .pex file for the Urho2d engine to load?
I know it can be opened in an XML file known as ParticleEmitterConfig tag, but that is all I could find in this content.
Thanks

Unity Resources.Load <Sprite> return null

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.

Unity C#; What's the reason of Resources.load(PNG file path) always return null?

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

Using an image in the resource folder in a function which takes file path to image as parameter

I'm developing a winform application. It has a reference to a dll library. I want to use a function 'PDFImage' in this library. This function is used to put images into a PDF documnent. The function 'PDFimage' has an argument 'FileName' of type String which takes the file location of the image.
Now I have to put the image as a separate file with the .exe file created after the project is built. This is not convenient for me. What I do now is I mention the file name of the image as the function parameter like 'Flower.jpg'. And I have kept the image in the \bin\debug folder.
I don't want to do it like this as this needs the image file to be placed seperately with the executable file.
What I am trying to do is as follows:
I added the image files to the Resources folder as existing item. Now, to call the function PDFImage, I need to pass the file name as argument. How can I do this?
I have the source code of dll with me. Is it better to modify the source code as required and create another dll rather than what I am doing now?
See if this helps;
string apppath = Application.StartupPath;
string resname = #"\Resource.bmp";
string localfile = "";
localfile = apppath + resname;//Create the path to this executable.
//Btw we are going to use the "Save" method of Bitmap class.It
//takes an absolute path as input and saves the file there.
//We accesed the Image resource through Properties.Resources.Settings and called Save method
//of Bitmap class.This saves the resource as a local file in the same folder as this executable.
Properties.Resources.Image.Save(localfile);
MessageBox.Show("The path to the local file is : " + Environment.NewLine + localfile + Environment.NewLine +
"Go and check the folder where this executable is.",
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
//localfile is the path you need to pass to some function like this;
//SomeClass.Somefunction(localfile);
Hope this helps and here is a sample if you need.
All you can do with that is get the resource, save it to a file (temporary one may be) and then pass the filename to the function. Most function that take a file in .net also take a stream, so if you have control of both sides, I'd do that and then you don't have to mess about with the file system.

Categories