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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by XaXa123 · May 30, 2013 at 06:53 PM · c#javascriptrewriting

Rewriting script from JavaScript to C#

Need help. I try to rewrite script from JavaScript to C#. I run my project but crosshair does not shows. There is the JavaScript:

 @script ExecuteInEditMode()
 var crosshairPreset : preset = preset.none;
  
 var showCrosshair : boolean = true;
 var verticalTexture : Texture;
 var horizontalTexture : Texture;
  
 //Size of boxes
 var cLength : float = 10;
 var cWidth : float = 3;
  
 //Spreed setup
 var minSpread : float = 45.0;
 var maxSpread : float = 250.0;
 var spreadPerSecond : float = 150.0;
 
  
 private var temp : Texture;
 private var spread : float;
  
 function Update(){
         //Used just for test (weapon script should change spread).
         if(Input.GetButton("Fire1")) spread += spreadPerSecond * Time.deltaTime; 
         else spread -= spreadPerSecond * 2 * Time.deltaTime;    
         
 }
 function OnGUI(){
         if(showCrosshair && verticalTexture && horizontalTexture){
                 var verticalT = GUIStyle();
                 var horizontalT = GUIStyle();
                 verticalT.normal.background = verticalTexture;
                 horizontalT.normal.background = horizontalTexture;
                 spread = Mathf.Clamp(spread, minSpread, maxSpread);
                              
                 
                 
                         //Horizontal
                         GUI.Box(Rect((Screen.width - cWidth)/2, (Screen.height - spread)/2 - cLength, cWidth, cLength), temp, horizontalT);
                         GUI.Box(Rect((Screen.width - cWidth)/2, (Screen.height + spread)/2, cWidth, cLength), temp, horizontalT);
                         //Vertical
                         GUI.Box(Rect((Screen.width - spread)/2 - cLength, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
                         GUI.Box(Rect((Screen.width + spread)/2, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
                 
         }
 }

There is the C#: using UnityEngine; using System.Collections;

 public class Proba : MonoBehaviour {
     bool  showCrosshair = true;
     public Texture2D verticalTexture;
         public Texture2D horizontalTexture;
     public float cLength = 10;
     public float cWidth = 3;
     //Spreed setup
     public float minSpread = 45.0f;
     public float maxSpread = 250.0f;
     public float spreadPerSecond = 150.0f;
     
     
     private Texture temp;
     private float spread;
     public GUIStyle verticalT;
     public GUIStyle horizontalT;
     
     // Update is called once per frame
     void Update () {
                 //Used just for test (weapon script should change spread).
         if(Input.GetButton("Fire1")) spread += spreadPerSecond * Time.deltaTime; 
         else spread -= spreadPerSecond * 2 * Time.deltaTime;   
     
     }
     void OnGUI(){
         if(showCrosshair && verticalTexture && horizontalTexture){
                 //var verticalT = GUIStyle();
                 //var horizontalT = GUIStyle();
                 verticalT.normal.background = verticalTexture;
                 horizontalT.normal.background = horizontalTexture;
                 spread = Mathf.Clamp(spread, minSpread, maxSpread);
 
                
                         
                         //Horizontal
                         GUI.Box( new Rect((Screen.width - cWidth)/2, (Screen.height - spread)/2 - cLength, cWidth, cLength), temp, horizontalT);
                         GUI.Box( new Rect((Screen.width - cWidth)/2, (Screen.height + spread)/2, cWidth, cLength), temp, horizontalT);
                         //Vertical
                         GUI.Box( new Rect((Screen.width - spread)/2 - cLength, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
                         GUI.Box( new Rect((Screen.width + spread)/2, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
                 
         }
 }
 }
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 robertbu · May 30, 2013 at 06:54 PM 1
Share

What does "does not work" mean?

avatar image XaXa123 · May 30, 2013 at 07:17 PM 0
Share

The crosshair does not shows.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by PAEvenson · May 30, 2013 at 07:48 PM

add [ExecuteInEditMode] to the class?

 [ExecuteInEditMode]
     public class Proba : MonoBehaviour {
     ...
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 XaXa123 · May 30, 2013 at 08:11 PM -1
Share

PAEvenson Thanks

avatar image Graham-Dunnett ♦♦ · May 30, 2013 at 08:43 PM 0
Share

@XaXa123 - if PAEvenson helped you show your appreciation by up-voting or accepting their solution. Posting a new answer won't help anyone.

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

15 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

Related Questions

Movement script, moving up when moving positively in z axis 1 Answer

snap object to object in game view ? 1 Answer

How to change game object axis according to the camera rotation? 0 Answers

How to keep the default axis as it is after camera rotation? 1 Answer

Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable 3 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