Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Tim Cooper · Jul 04, 2010 at 02:19 AM · shadermaterialgraphicsimportasset

Creating a Material from shader string

Hi,

I am trying to create a material by using a string for the shader parameter as described here: http://unity3d.com/support/documentation/ScriptReference/Material.Material.html (the first constructor).

This works for shaders that are very simple and have no lighting, but slightly more complex shader can not be loaded this way. I have looked through all the documentation and have not been able to find a description of what is and what isn't allowed in materials created from string.

This is the shader I am trying to load (taken from the shader lab documentation):

Shader "Testshader" { SubShader { Pass {

CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_fog_exp2 #include "UnityCG.cginc"

struct v2f { V2F_POS_FOG; float3 color : COLOR0; };

v2f vert (appdata_base v) { v2f o; PositionFog( v.vertex, o.pos, o.fog ); o.color = v.normal * 0.5 + 0.5; return o; }

half4 frag (v2f i) : COLOR { return half4( i.color, 1 ); } ENDCG

 }

} Fallback "VertexLit" }

And I am loading it this way:

renderMat = new Material( File.ReadAllText( Application.dataPath + "/Data/Temp/Resources/testshader.shader" )  );

It is very important that I be able to load a shader from a string for the work I am currently doing.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image taoa · Jul 06, 2010 at 09:19 AM 0
Share

You're probably not doing the right thing somewhere if you say you $$anonymous$$UST have your shader CG code directly into your script code. As $$anonymous$$said, you can't have CG in strings, so think about what you're doing and you'll probably find that you can do it another way.

avatar image Tim Cooper · Jul 07, 2010 at 03:10 PM 0
Share

I'm doing something very tricky...This isn't standard content creation.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Tim Cooper · Jul 07, 2010 at 03:24 PM

Due to this being 'not supported' I worked around the issue by saving my shader to a file in the project directory then force reloading it like so:

Directory.CreateDirectory(  Application.dataPath + _EditorResourcePath );
File.WriteAllText( Application.dataPath + _EditorResourcePath + "/workingshader.shader", shaderString );            
Shader currentShader = Resources.Load( "workingshader" ) as Shader;
var path = AssetDatabase.GetAssetPath(currentShader);
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
Material m = new Material( currentShader );

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by jonas-echterhoff · Jul 06, 2010 at 08:58 AM

You can not load shaders which contain Cg programs from a string at runtime, as the Cg compiler is only part of the Unity editor, not of the player. Possibly, you may be able to compile the shader in the editor, and then load the compiled shader from a string (in the inspector for the shader, click "Open compiled shader").

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tim Cooper · Jul 07, 2010 at 03:10 PM 0
Share

I am trying this from an editor script. But I do understand when you say it may not be supported. I worked around this issue as described in the answer below.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Imported Materials Are Null 0 Answers

Importing Maya assets with applied textures shown 0 Answers

Mesh Obj export/import WITH SHADER REFERENCE 3 Answers

Is there a way to get hard pixelated shadows? 1 Answer

How can I merge shader effects inside Unity, not an external program? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges