x


Assigning meshes & materials from Assets, how?

I know this must be plainly simple, but still it eludes me:

I want to create a mesh and/or material (in an Editor script), and I want to assign a mesh from my assets. Not necessarily in Resources folder mind you, just some random asset.

something like:

gameObject.GetComponent(MeshFilter).sharedMesh = ?LoadAsset? ("big_gun_mesh");

Is this possible?

more ▼

asked Jul 28 '10 at 08:28 PM

DaveA gravatar image

DaveA
26.4k 151 171 256

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You'll want to use AssetDatabase.LoadAssetAtPath:

http://unity3d.com/support/documentation/ScriptReference/AssetDatabase.LoadAssetAtPath.html

It's similar to Resource.Load, but takes paths relative to the project folder, not assets as you may first guess

more ▼

answered Jul 28 '10 at 08:38 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 252

Sounds promising, but is there any way to not use a path? Eg. when you go to the MeshRenderer, you can drop-down a (non-pathed) list of meshes to choose. That would be nice. Otherwise, I have to crawl my project folder looking for the mesh, which in fact may be a sub-mesh.

Jul 28 '10 at 08:49 PM DaveA

In that case, I'd just make a wizard with a public mesh property, drag the right mesh in, and do the processing after create

Jul 28 '10 at 09:15 PM Mike 3

I'm really trying to avoid that 'drag' step all together. I'm making a script to repair 'missing mesh' and 'missing material' problems. I have a ton of them, too much dragging each time.

Jul 28 '10 at 09:31 PM DaveA

In that case, the only thing I can think of is recurse through the assets using functions from System.IO.Directory, then render them in a selectable list

Jul 28 '10 at 10:02 PM Mike 3

This can't be really interactive. Actually, I have made some headway. I'm assuming a particular path and all files .dae. So when I run into a game object with a MeshRenderer but no mesh, I load the dae with the same name as the parent of this object, then look in the loaded objects for one that matches the current gameobject's name, with type of Mesh. I then assign this to the sharedMesh property on the mesh filter. This actually seems to work but I'm not thrilled, because the results seem to have some serious clipping plane problems (original, undamaged file had no problems). Also (see next..

Jul 28 '10 at 10:12 PM DaveA
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3315
x1352
x807
x417
x92

asked: Jul 28 '10 at 08:28 PM

Seen: 2090 times

Last Updated: Jul 28 '10 at 08:28 PM