• 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
1
Question by chrishughes2d · Jul 05, 2013 at 08:00 AM · texturemeshmaterial

How to load and render a sprite in script?

Hi folks,

I've just come over from XNA and wish to port one of my 2D projects onto Unity. I found this article from a former XNA-er which suggested creating a script which instantiates the XNA game class and acts as a proxy between it and Unity. While I appreciate the limitations of this technique, it interests me enough to give it a shot.

XNA provides a SpriteBatch class for sprite rendering, but the article above suggests using a custom mesh in Unity. I've tried and failed to get this to work. I've included my code below, which throws a NullReferenceException in the material constructor. I'm very lost, and would appreciate any insights you can offer.

Apologies for the newbie question!

Cheers!

 using UnityEngine;
 
 public class GameManager : MonoBehaviour
 {
     Mesh mesh;
     Shader shader;
     Material material;
 
     // Start
     void Start()
     {
         mesh = new Mesh();
         shader = new Shader();
         material = new Material(shader);
         material.mainTexture = Resources.Load("Content/Texture") as Texture;    
     }
     
     // Update
     void Update()
     {
         Vector3[] vertices = new Vector3[4];
         vertices[0] = new Vector3(540, 260, 0);
         vertices[1] = new Vector3(740, 260, 0);
         vertices[2] = new Vector3(540, 460, 0);
         vertices[3] = new Vector3(740, 460, 0);
 
         Vector2[] uvs = new Vector2[4];
         uvs[0] = new Vector2(0, 0);
         uvs[1] = new Vector2(0, 0);
         uvs[2] = new Vector2(0, 0);
         uvs[3] = new Vector2(0, 0);
 
         int[] triangles = new int[6];
         triangles[0] = 0;
         triangles[1] = 1;
         triangles[2] = 2;
         triangles[3] = 2;
         triangles[4] = 1;
         triangles[5] = 3;
         
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         mesh.uv = uvs;
         
         Graphics.DrawMesh(mesh, Matrix4x4.identity, material, 0);
     }
 }

Comment
Add comment
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

1 Reply

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

Answer by JohnnySunshine · Jul 05, 2013 at 01:17 PM

You can't really instantiate a shader like that, have a look at this: Shader.Find

Also, you should put the mesh construction code in the Start function -- Update is called every frame, and you probably don't need to regenerate it so often. The UV's are off, too (they're all zeroed out).

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 chrishughes2d · Jul 05, 2013 at 11:20 PM 0
Share

Thanks Johnny. It's working now. I appreciate your help.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to save runtime generated mesh as a texture ? 1 Answer

Material doesn't have a color property '_Color' 4 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Different prefabs with same mesh but different textures(materials). 1 Answer

Which is more efficient? 1 Answer


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