Loading 3d models at runtime in Unity3d
IMPORTANT UPDATE! With Unity 3 there were some changes in the API and code described below doesn't work. Here's a new version, that works with Unity 3: obj (v1.2). Now, instead of creating a new object, attach the OBJ script to a game object and add the path to the .obj file in the inspector.
This post is an addition to the last series I wrote about dynamic content loading in Unity3D.
When building a dynamic application, every now and then you might need to load some 3d models at runtime. Either because there's a database with models you want to use or just because you want someone to update the models without rebuilding the project. I actually had this situation a few weeks ago. Surprisingly, this option does not come with Unity out of the box.
Asset bundles and resource folders
But first thing first. I said there's no ready-to-use way to load 3d models at runtime, but it's not 100% true. In fact, there are two options: asset bundles and resource folders. These methods are discussed in depth here.
They can both be useful in some cases, but they both have one fundamental drawback: in order to create them, someone needs to open the project in Unity, import the 3d models, create the bundles or resource folders and then export the whole thing again. To create an AssetBundle you need to run a script in the editor, while the Resource Folders... honestly, I failed to even make them work!
In brief, it's all too complicated for what I was looking for.
A straightforward solution
What I needed and wanted was easy and simple: to load a 3d object (geometry + some materials and textures) at runtime, using just a URL as parameter. The only solution seemed to write my own importer.
Basically, I had the choice between Collada and Wavefornt OBJ. I would choose OBJ any time of the day because it's a simple, concise plain-text format, while Collada is bloated and is XML-based.
It's not that I hate XML (although I'm not a big fan either) but in order to parse XML you need to include a pretty weighty DLL in your *.unity3d file, around 850Kb, which in this case (and in many others) defeats the purpose. Still, it's good to know that it possible, and there are situations when it's ok to use it. If you want to learn more about Unity3D and XML there's a awesome article on this topic by Paul Tondeur.
It turned out that while it's not rocket science to write an OBJ importer, it's not exactly banal either. I spent a few days coding it so I thought I'll share this with everyone - maybe someone will make good use of it.
Here's a package with the source code (v1.1) (or a version that works in Unity 3 (v 1.2)), along with a simple scene demonstrating how it works. In fact it couldn't be simpler. All you have to do is to create an instance of the OBJ class and start a coroutine to load the contents, like this:
-
string path = "http://www.everyday3d/unity3d/obj/monkey.obj";
-
StartCoroutine(obj.Load(path));
Supported features
- Vertex, normals and UV data
- Multiple objects per file
- Vertex groups and multiple materials per object
- MTL files, diffuse and specular materials
- Basic textures
And it's all at a cost of ~12Kb extra added to your final file!
Testing and the universality of OBJ format
In the 3D world the OBJ format is nearly universal - I think that every 3D editor in existence is able to export to this format. This also means that Wavefront files come in many different flavors.
I tested the code against models created with Blender and against common sense assumptions on how an OBJ file can be constructed. If it doesn't work with files exported from your editor send me the OBJ file, and if possible I'll update the code. It's released under the MIT license so feel free to use it in your projects, commercial or not.
That's it! I hope you'll find it useful!




Nice- I’ve seen a few requests for something like this, though I haven’t needed it myself. I suggest you assign the category “Unity3d” to the post.. You also might want to create a page for it on the Unify Wiki, which can just link back to here if you prefer. (There’s already an OBJExporter script there.)
Great idea! I’ve tried working with Asset Bundles, and although I got them to work, I found the process a bit clunky. And as you mentioned, you have to open up the Unity editor.
I’ll be giving this a try in the future.
Nice work ! I tried your demo scene and got an error parsing the remote obj (the Monkey.obj from your website). Is it related to indie vs pro version of Unity ?
@Arnaud I’m double checking this right now. It should work both in Indie and in Pro. Can you paste the error message?
@Matt Thanks, I forgot to add categories and tags :) I will post it on Unify as soon as I’m sure that it works on the free version as well.
[...] in Unity3D??? Check this Quer carregar modelos 3d durante a execução no Unity??? Veja o link. Share and [...]
This is very cool! I’ll be testing this this week as Asset Bundles seemed tougher than needed to implement.
As always, excellent post here! I hope to get some spare time to actually try it out instead of just reading through it.
We’re doing a broadcast on Wednesday about the impact of Unity Google Native Client. If you can join in please drop me an email at mani at diamondtearz.org
Hello,
Ia m trying your package and the main scene is not working. I have this error in the console :
FormatException: Unknown char: .
System.Double.Parse (System.String s, NumberStyles style, IFormatProvider provider)
System.Single.Parse (System.String s)
System.Convert.ToSingle (System.String value)
OBJ.cf (System.String v) (at Assets\OBJ\src\OBJ.cs:115)
OBJ.SetGeometryData (System.String data) (at Assets\OBJ\src\OBJ.cs:83)
OBJ+c__CompilerGenerated0.MoveNext () (at Assets\OBJ\src\OBJ.cs:46)
@krys64 Can you send me the OBJ file you are loading? (bartek at everyday3d com)
I am using your package with your file : http://everyday3d.com/unity3d/obj/monkey.obj
I have changed nothing. I see that :
http://webcache.googleusercontent.com/search?q=cache:p3iAr1PRiqwJ:markmail.org/message/rgcv25vopmpvzbeq+FormatException:+Unknown+char:+.&cd=1&hl=fr&ct=clnk&gl=fr
My computer is an european system
@krys64 Thanks, that helps a lot! I’ll look into that as soon as I can.
@kyrs64 I posted an updated version of the importer which contains a fix to this problem.
Thank you, i’ll test end post a news on my site ;)
[...] -Le site [...]
Thanks you. i’ll test this
Hello, i am trying to convert your script for read a local .obj file.
I have modified the OBJ.cs script and i have replaced “WWW” by “streamReader” but to the line 61 i can’t load the texture. What is the syntax please. THX :)
I am stupid, i reimport the package and i have change “http” by “fil”e and it’s work ! :)
Hi, this is a great post, and will be very useful to me, but I can’t get it to work. I imported the package, and then changed the “Obj Import Demo” script to:
using UnityEngine;
using System.Collections;
public class ObjImportDemo : MonoBehaviour {
public string filename;
void Start () {
string path = “http://www.everyday3d.com/unity3d/obj/monkey.obj”;
OBJ obj = new OBJ();
StartCoroutine(obj.Load(path));
}
void Update () {
}
}
When I run the scene, I get the following error:
You are trying to load data from a www stream which had the following error when downloading.
couldn’t connect to host
UnityEngine.WWW:get_data()
UnityEngine.WWW:get_data()
c__CompilerGenerated0:MoveNext() (at Assets\OBJ\src\OBJ.cs:47)
[..\..\Runtime\Export\WWW.cpp line 771]
This is a great idea and something I’ve been looking for for a while. I am having some trouble getting it working though. It works great with the given .obj file and www location. But as soon as I use either a local file (file///:) or a test file I put on the web I get an error:
“ArgumentNullException: Argument cannot be null.
Parameter name: key
System.Collections.Generic.Dictionary`2[System.String,UnityEngine.Material].get_Item (System.String ) [0x00000]
GeometryBuffer.PopulateMeshes (UnityEngine.GameObject[] gs, System.Collections.Generic.Dictionary`2 mats) [0x00197] (at Assets\OBJ\src\GeometryBuffer.cs:145)
OBJ.Build () (at Assets\OBJ\src\OBJ.cs:236)
OBJ+c__CompilerGenerated0.MoveNext () (at Assets\OBJ\src\OBJ.cs:63)”
this is cause by the line 145 in GeometryBuffer.cs
I was wondering if there is a specific file structure the .obj and .mtl and texture files need to be in.
The other problem I have is that it will work, but the object it creates is empty. I think the previous problem is because a *.mtl file was not available.
Hi, well first my huge thank you as it solved quite a big issue I got with Unity! :)
I was trying to use it and I had to convert my GoogleSkected OBJ files into Blender… and it worked quite well. But for a little more complex scene, I got this weird message:
KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.String,UnityEngine.Material].get_Item (System.String ) [0x00000]
which I don’t know what it depends for.
Many many thanks!
Giancarlo
Hi,
converted my obj files using blender… and I used your C# scripts to load an object, but the LOG files returns me these warning/errors and I believe the C# Load function doesn’t complete as it returns a NULL REFERENCE, moreover sometimes I get an error that the edges has to be multiple of 3.:
http://localhost/OBJs/DisplayCases/wwwdisplaycase1.obj
UnityEngine.Debug:Log(Object)
wwwManager_script:wwwRequest(String)
(Filename: ..\..\Runtime\Export\Generated\BaseClass.cpp Line: 1783)
wwwLoadObjects
UnityEngine.Debug:Log(Object)
wwwManager_script:wwwLoadObjects(String)
wwwManager_script:wwwRequest(String)
(Filename: ..\..\Runtime\Export\Generated\BaseClass.cpp Line: 1783)
NullReferenceException
wwwManager_script.wwwLoadObjects (System.String addrs)
wwwManager_script.wwwRequest (System.String param)
(Filename: Line: -1)
thank you so much for any help you can provide me.
Giancarlo
@Giancarlo if it says “edges has to be multiple of 3″ that means that the OBJ file contains quads, and Unity3D doesn’t work with quads. What you need is to triangulate your model before exporting it – depending on your 3d software this function can be in different places, so you need to look for it, but it’s usually called either “Triangulate” or “Convert quads to tris” or something similar…
@Bartek,
Thank you so much! I’ll check that out. Hopefully, blender should have something similar.
GC.
Hello Bartek, I converted my models into OBJ using blender as you did. When I save them, I chose TRIANGULATE to generate triangles but still when I try to upload the model with your scripts I get this error message: (any help, THANKS)
ArgumentNullException: Argument cannot be null.
Parameter name: key
System.Collections.Generic.Dictionary`2[System.String,UnityEngine.Material].get_Item (System.String ) [0x00000]
GeometryBuffer.PopulateMeshes (UnityEngine.GameObject[] gs, System.Collections.Generic.Dictionary`2 mats) [0x00197] (at Assets\Standard Assets\OBJ\src\GeometryBuffer.cs:145)
OBJ.Build () (at Assets\Standard Assets\OBJ\src\OBJ.cs:237)
OBJ+c__CompilerGenerated0.MoveNext () (at Assets\Standard Assets\OBJ\src\OBJ.cs:63)
Hy!
Can you help me?
I exported obj in blender.
2 material name “also” and “felso”.
Open obj file your script, but Material elements name always Specular.
How to fix it?
Thanks!
Sorry my English!
@Kalman – that sounds more like the name of the shader used. Can you send me a screenshot from your inspector view, where you see the object and the materials? Thanks, B
Hello, I have been testing the code with triangulated models and it works perfect. I have also been trying to reload a different model into the existing mesh. First I cleared the vertex and triangle data, which worked fine. But the problem is the data of the materials, when I try to reload another model I get this error message:
KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.String,UnityEngine.Material].get_Item (System.String ) [0x00000]
GeometryBuffer.PopulateMeshes (UnityEngine.GameObject[] gs, System.Collections.Generic.Dictionary`2 mats) [0x00244] (at Assets\Scripts\GeometryBuffer.cs:165)
OBJ.Build () (at Assets\Scripts\OBJ.cs:239)
OBJ+c__CompilerGenerated0.MoveNext () (at Assets\Scripts\OBJ.cs:66)
Could anyone explaime how to fix the materials problem?
Thanks for any help
Hi Bertek,
I tried to move onto Unity3D 3, the new release and apparently they SEALED the GameObject class. that means that the inheritance from GameObject is not allowed if the class you specify is derived from Monobehaviours.
In your C# Code you declare OBJ as
public class OBJ: GameObjects
and this statement causes the compiling error.
Any idea on how to fix this up?
Thank.
Giancarlo
Hi Bartek,
I fixed problem, here is 2 screenshots.
http://kepfeltoltes.hu/101005/specular_www.kepfeltoltes.hu_.jpg
Material names always Specular.
OBJ.cs
if(md.illumType == 2) {
m = new Material(Shader.Find(“Specular”));
m.SetColor(“_SpecColor”, md.specular);
m.SetFloat(“_Shininess”, md.shininess);
m.name = md.name; //Add this!!
} else {
And Whoala material name fixed.:)
http://kepfeltoltes.hu/101005/fixed_www.kepfeltoltes.hu_.jpg
Hi Bartek and commenters having trouble compiling:
Just a note: typo in obj-url above (maybe on purpose? :)
http://www.everyday3d/unity3d/obj/monkey.obj
Should be
http://www.everyday3d.com/unity3d/obj/monkey.obj
For people wanting to try out the OBJ example in Unity3D 3.0, here’s a patch which seem to work okay (I’m able to load and view monkey.obj): http://pastebin.com/1fiTLXKS
Hi Tim Knip, thanks a lot for the patch, I downloaded it but somehow it doesn’t work :( … I am a bit confused.. I followed the link at pastbin… but I see 2 source codes one of the top of the page, and the onther under “Submit a correction…” section. Which one of the two should I download?
Thanks,
GIancarlo
Hi TimKnip, I used both your versions for OBJ.cs but I get this error (it was working with the 2.3 version):
wwwManager_script:wwwSERVERLoadObjects(String, Boolean)
(Filename: ..\..\Runtime\Export\Generated\BaseClass.cpp Line: 2535)
NullReferenceException: Object reference not set to an instance of an object
at Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) [0x00000] in :0
at Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) [0x00000] in :0
at Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value) [0x00000] in :0
at wwwManager_script.wwwSERVERLoadObjects (System.String addrs, Boolean loaded) [0x00000] in :0
(Filename: Line: -1)
About to parse url: http://localhost/OBJs/Rooms/wwwplan.obj
luccheck6
SecurityException: No valid crossdomain policy available to allow access
at (wrapper managed-to-native) UnityEngine.WWW:get_bytes ()
at UnityEngine.WWW.get_text () [0x00000] in :0
at OBJ+c__Iterator0.MoveNext () [0x00000] in :0
(Filename: Line: -1)
Hi TIM KNIP, I was wondering about your patch as the class OBJ is not/can’t be derived from GameObject anymore, it has to be derived from a generic Object or an Asset, so I wonder how do you use that patch you were suggesting.
Thanks,
Giancarlo
Hello guys’
I need to attach to the new gameObject a mesh collider, how can I access the created mesh in the OBJ? I went through the code but can’t see where you create the mesh..
Thanks.
Giancarlo
@Giancarlo The mesh is in the meshFilter component – just like any regular mesh. I believe that if you attach a MeshCollider component to this game object it should work.
Hi Bartek thank for your reply
but I had to modify your package as you originally derived OBJ class from GameObject, which is not possible in the release 3. So, now I don’t have getComponent anymore…and I had to overcome in a different way. I tried to derive your class from MonoBehaviour or Asset but I got Errors at runtime.
Thanks.
@Giancarlo – MonoBehaviour should be ok if you can’t derive from GameObject. Most errors come from the fact that it might be referencing some methods and properties specific to GameObject.
@Bartek, thank you yes I also tried to derive it from a MonoBehaviour and apparently it works, but I get a Warning message when I call obj = new OBJ(); :
You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor()
OBJ:.ctor()
@Bartek HI,
bartek sorry to bother you, but in the OBJ.cs at line 246 this is the line:
ms[0] = obj.gameObject;
but OBJ is already a GameObject, is gameObject (small g) a member of GameObject?
Best,
Giancarlo
This script doesn’t work in Unity 3, ’cause of the monobehavior, so if somebody fixed it exactly, please could you help me?! Thanks in advance
@Raimond: Hi yes if you scroll up on the list you can see a patch…. anyway it is enough that the OBJ doesn’t derive from Monobehaviour and/or asset. you can use it as a single class.
Hi Bartek, if I edit my OBJ model with Blender and I attach a texture… will I be able to see that texture in Unity3 too??
I’m having some troubles to be honest.
Thansk.
@Bartek: Hi Bartek, I’ve just found that loading a texture using WWW doens’t work if you are developing an application for web. :( That’s what I am doing I’m afraid, so I wonder if you might have any suggestion to link the texture to the object… different from the WWW which I think you use.
Cheers,
Giancarlo
I downloaded your package and in turn i got this error
Assets/OBJ/src/OBJ.cs(7,14): error CS0509: `OBJ’: cannot derive from sealed type `UnityEngine.GameObject’
Any ideas where i am going wrong ?
Regards,
Ashok Srinivasan
@Srinivasan: yes , if you are using Unity3 you can’t derive from GameObject. They don’t allow it…so you just get rid of the it and use it as a normal class. I did it and it “works”.
@Giancarlo: Please explain what have you done with script to make it works in Unity 3. I have same problem like Srinivasan has.
As Giancarlo rightly pointed out, the code is now broken because of a change in API in Unity 3. I will try to post an updated version that will work, but unfortunately I don’t have the time to do this in the coming weeks (very busy!)
@Bartek: Thanks for answer :)
May be you can indicate, in what area (areas) of the code should I make changes for proper work in Unity 3?
3.1 把脚本OBJ.cs改为继承MonoBehaviour后,然后再根据提示稍微改下是可以用的。
i am trying to use it, i am new to unity…i have unity 3.0 installed, i downloaded i the package “obj.1.1.unitypackage” from everyday3d, when i import it, this error comes -
Assets/OBJ/src/OBJ.cs(7,14): error CS0509: `OBJ’: cannot derive from sealed type `UnityEngine.GameObject’
please guide me in steps as what should i do as a lehman.
@saurabh: We should wait for changes in this code because of new version of the Unity :)
@Once after i changed the class from gameobject to monobehaviour, what next then, don’t know how to continuse
你好 once,改变继承之后会出现很多提示,一个我也看不懂,能否发一个改动之后的文件 :)
@Jonny, As for me, I see only hieroglyphes after the “continuse” :)
@Rustam, seams it didn’t work the Class monobehaviour can’t use “New” to create an instance,so i try to inhert OBJ class from ScriptableObject Class(inherit from Object class), but still have problem with it the gameObject fucntion is belongs to GameObject class which is inherit from Object . help~~~~
@Everyone Here’s a new version that works with Unity 3 obj (v1.2). Now, instead of creating a new object, attach the OBJ script to a game object and add the path to the .obj file in the inspector. Have fun!
great let me try thank you a lot :)
@Bartek, really appreciated. Thank you so much!
oh i got a problem when use this,when i export it as obj from 3d max it didn’t work
the obj file is like this :
# 3ds Max Wavefront OBJ Exporter v0.97b – (c)2007 guruware
# File Created: 28.12.2010 14:45:53
mtllib testobj.mtl
#
# object Box001
#
v -2.562998 -4.306799 0.000000
v -2.562998 4.306799 0.000000
v 2.562998 4.306799 0.000000
v 2.562998 -4.306799 0.000000
v -2.562998 -4.306799 5.110198
v 2.562998 -4.306799 5.110198
v 2.562998 4.306799 5.110198
v -2.562998 4.306799 5.110198
# 8 vertices
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 -1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 0.000000 1.000000 0.000000
vn -1.000000 0.000000 0.000000
# 6 vertex normals
vt 1.000000 0.000000 0.000000
vt 1.000000 1.000000 0.000000
vt 0.000000 1.000000 0.000000
vt 0.000000 0.000000 0.000000
# 4 texture coords
g Box001
usemtl wire_088177026
s 2
f 1/1/1 2/2/1 3/3/1 4/4/1
s 4
f 5/4/2 6/1/2 7/2/2 8/3/2
s 8
f 1/4/3 4/1/3 6/2/3 5/3/3
s 16
f 4/4/4 3/1/4 7/2/4 6/3/4
s 32
f 3/4/5 2/1/5 8/2/5 7/3/5
s 64
f 2/4/6 1/1/6 5/2/6 8/3/6
# 6 polygon
don’t know what is wrong help
Hi Bartek,
Firstly thankyou for such a wonderful kick start to a problem I am currently addressing, many choices available but yours was for the right platform (Unity3d) so I thought I could build upon the hard work you had already undertaken so far, I hope some of this is a help and not a hinderance and believe me I am greatfull for the headstart, excellent work.
I found certain issues with diffently sourced OBJ files, from max, sketch-up and turbosquid etc, so these ammendments helped deal with issues surrounding the OBJ spec. So I thought it only right to share and report back with fixes that I had made:-
Here is a link to my changes:
OBJ.cs at http://pastebin.com/yqQjyHUg
GeometryBuffer.cs at http://pastebin.com/YPSYPi73
A list of alterations:-
ALTERATION 0:- Cater for Local Files also.
ALTERATION 1:- Major Problems with different OBJ file format whitespace.
ALTERATION 2:- Rough Fix to deal with quads and polys there may be better methods but seems to work, even with textures so far.
ALTERATION 3:- I found an OBJ on turbosquid with no material library reference so Updated GeometryBuffer PushGroup() adding g.materialName = “default”.
ALTERATION 4:- Lets say the user moved the material library elsewhere, I created error handling and ReturnMaterialNames()function in GeometryBuffer.cs to create default materials with correct usemtl names
ALTERATION 5:- Added Recalculate normals for OBJ file without normals.
ALTERATION 6:- Added to name materials correctly
ALTERATION 7:- Added CalculateTangents Function to cater for bumpmaps on the mesh etc. Will be usefull at a later stage (still resolving materials import section on this One at present)
This works great on UnityV3 Pro but should still work (if it previously worked) fine on indie, don’t think I added anything to irregular.
I also do realise that according to your site this script might is released under MIT, so wondered if I could mail you about the GPL project I am currently working on and talk about it’s inclusion, this is a not for profit application an will serves as a GPL Educational Application with open source libraries i.e. (unity project code).
Hope this helps, Jon Martin
[...] have so far extended Bartek Drozdz OBJ importer V1.2 at Everday3d.com by helping it achieve the [...]
Thanks…we have a lot in common…20 year web vetran.just switched to unity 3D and it is a gamechanger..gone are the gaming consoles????..broadband for all!!
anyway…quick tip for me to get this working…
The server MIME type has to set for .obj / .mtl..
after I did that..it worked..mat files a little wacked, but thats cake…
kongregate game site has a 20 mb initial size limit.
this really helps for us poor new developers!!!
THANX!!!!!!
Thanks for the update Jon. I’m having a bit of trouble with everything defaulting, and then it not finding the default material in the library. I’m using the OBJ here: http://www.pandoodle.com/demos/Face/base_1500.obj (caps matters).
The result hangs on this error:
KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.String,UnityEngine.Material].get_Item (System.String key)
GeometryBuffer.PopulateMeshes (UnityEngine.GameObject[] gs, System.Collections.Generic.Dictionary`2 mats) (at Assets/OBJ/src/GeometryBuffer.cs:197)
OBJ.Build () (at Assets/OBJ/src/OBJ.cs:302)
OBJ+c__Iterator0.MoveNext () (at Assets/OBJ/src/OBJ.cs:93)
Any help would be greatly appreciated! Thanks!
Hello, in response to the above you could try the runtime import library alpha version 0.4, I uploaded last friday
http://jon-martin.com/?cat=13
it may or not solve this problem, not sure about the webside of the loading yet as it has been untested for that scenario but it terms of referencing materials libs etc it should handle much better.
Thanks again Bartok, I would be interested in your feedback on my extensions so far, currently working on project save with reflection, having a few problems identifying if a GO is a prefab at runtime.
Hello Sir,
when i simply run your package on my system in unity, the red monkey appears… i downloaded the obj money from this link:
http://www.everyday3d.com/unity3d/obj/monkey.obj
and uploaded on my hosting, here -
http://www.cookasite.com/14j/obj-for-ubity/monkey.obj
i changed the path in the script…so now an error comes -
KeyNotFoundException: The given key was not present in the dictionary.
Why so ?? please help
Hi,
so far i’m trying this code but i’m getting an error and the error is
NullReferenceException: Object reference not set to an instance of an object
OBJ.SetGeometryData (System.String data) (at Assets/OBJ/src/OBJ.cs:120)
OBJ+c__Iterator0.MoveNext () (at Assets/OBJ/src/OBJ.cs:58)
as i’m very new to unity3d haven’t able to debug plz suggest to fix this problem.
thanks in advance
Hi,
the code works fine in unity but the material/texture does not show up in the iphone and iphone simulator. The mesh renders out black. How can I solve this issue? thanks.
I optimized the loader code:
// via profiler, discovered that Convert.ToSingle and Convert.ToInt32
// are very slow… so using float.Parse and int.Parse instead!
//
// memory usage during obj loading went down from 56 MB to 6.8 MB (8.2x improvement)
// and load time went down 1742ms to 514ms (3.4x improvement)
/*
private float cf(string v) {
return Convert.ToSingle(v.Trim(), new CultureInfo(“en-US”));
}
private int ci(string v) {
return Convert.ToInt32(v.Trim(), new CultureInfo(“en-US”));
}
*/
private float cf(string v) {
return float.Parse(v);
}
private int ci(string v) {
return int.Parse(v);
}
and the gist is on github
https://gist.github.com/1435032