Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Borzi · Sep 17, 2013 at 06:25 PM · c#texturejavaconversionvar type

Converting Script from Java to C#

I usually don't do this kind of stuff but could someone help me convert this? My biggest problem is to know what var type's h and w are. If someone could help me, it would be much appreciated!

 @script ExecuteInEditMode()
 var crosshair : Texture2D;
 public var widthcross : int;
 public var heightcross : int;
 function OnGUI () 
     {
     var w = crosshair.width/widthcross;
     var h = crosshair.height/heightcross;
     position = Rect((Screen.width - w)/2,(Screen.height - h )/2, w, h);
     GUI.DrawTexture(position, crosshair);
     }
     
Comment
Add comment · Show 3
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 numberkruncher · Sep 17, 2013 at 06:34 PM 0
Share

Questions like this are best asked in the Unity Forum since the title is not actually a question, and code conversion is rarely useful for future readers in Q&A format.

avatar image AndyMartin458 · Sep 17, 2013 at 06:50 PM 0
Share

just so you know, javascript is not java

avatar image Borzi · Sep 17, 2013 at 07:58 PM 1
Share

Sorry mate i'll remember that for next time. @Andy yeah I know I rushed the title a bit, but I mean it didn't really cause any confusion I guess.

2 Replies

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

Answer by numberkruncher · Sep 17, 2013 at 06:33 PM

Not tested, but something like the following should work:

 using UnityEngine;

 [ExecuteInEditMode]
 public class YourScriptName : MonoBehaviour {
     public Texture2D crosshair;
     public int widthcross;
     public int heightcross;

     void OnGUI() {
         // Only draw textures for repaint events.
         if (Event.current.type == EventType.Repaint) {
             int w = crosshair.width / widthcross;
             int h = crosshair.height / heightcross;
             Rect position = new Rect((Screen.width - w) / 2, (Screen.height - h) / 2, w, h);

             GUI.DrawTexture(position, crosshair);
         }
     }
 }
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 Borzi · Sep 17, 2013 at 06:40 PM 0
Share

Thanks! This fixed it :)

avatar image
2

Answer by clunk47 · Sep 17, 2013 at 06:34 PM

Define a class, and import System.Collections and UnityEngine. Instead of using something like var widthcross : int, in C# this would be public int widthcross. Public makes properties available for changing in the inspector, as well as accessible from other scripts. Be sure to name your C# script the same as your class.

Example.cs

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEditMode]
 
 public class Example : MonoBehaviour 
 {
     public Texture2D crosshair;
     public int widthcross;
     public int heightcross;
     float w;
     float h;
     Rect position;
     
     void OnGUI () 
     {
         w = crosshair.width/widthcross;
         h = crosshair.height/heightcross;
         position = new Rect((Screen.width - w)/2,(Screen.height - h )/2, w, h);
         GUI.DrawTexture(position, crosshair);
     }
 }
 
Comment
Add comment · Show 2 · 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 numberkruncher · Sep 17, 2013 at 06:37 PM 1
Share

Throw-away position rectangles are better handled within function scope since Rect is a value type. Same goes for the other 2 temporary variables w and h. Accessing member variables is slower than accessing local variables since this must first be dereferenced.

avatar image Borzi · Sep 17, 2013 at 06:40 PM 2
Share

This also works but the other guy answered first, thank you anyway, voted your answer up!

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

18 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 avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

I want to convert image data(Texture2D) to Base64 string and store in JSON file in Unity3D(C#). 0 Answers

Distribute terrain in zones 3 Answers

Convert a Java open source project to Unity C#? 1 Answer

64k limit when convert byte[] to base64 string 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