Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Zitoox · Oct 07, 2016 at 11:46 PM · c#generateserial

Generate key code

I want to make a Key Code generator. I am talking about something that would allow me to generate a code like xxxx-xxxx-xxxx, but i didn't found anything like that...

I am going to make a game where basically you solve puzzles using a computer (in game), so i wanted to make this part of the game, and i wanted to test it first in visual studio by making just a simple software just to see if it works, so i can "update" it to the game and make the system inside it.

As the player obviously needs to use the mouse and my screen have some buttons to click, i assume that i could use if (Input.GetButton("Fire1") and then make something below that, but i only found this:

 Random rnd = new Random();
 int month = rnd.Next(1, 13); // creates a number between 1 and 12
 int dice = rnd.Next(1, 7);   // creates a number between 1 and 6
 int card = rnd.Next(52);     // creates a number between 0 and 51

And i don't get where do i insert it. Could anyone help me? I would really appreciate any sugestion.

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

3 Replies

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

Answer by aditya · Oct 08, 2016 at 09:24 AM

Give this a try .. fully tested and i've tried to put comments wherever needed ...

 using UnityEngine;
 using System.Collections;
 using System.Text;
 
 public class keyCode : MonoBehaviour {
     private string[] alpb, num;
     void Start () {
         num = new string[10]{ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
         alpb = new string[26] {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
         Debug.Log (generateKey (3, 5)); // Just for the sake of a simple test
     }
     /*
      * nodeCount of 3 and nodeCharCount of 5 will produce something like this
      * yu67h-7uyh8-i8uy6
      */
     public string generateKey(int nodeCount, int nodeCharCount){
         //Shuffle our arrays first so that every time we get a random key
         shuffleArray<string> (num);
         shuffleArray<string> (alpb);
         nodeCount = Mathf.Clamp (nodeCount, 2, 5);
         nodeCharCount = Mathf.Clamp (nodeCharCount, 3, 5);
         int numIndex = 0, alpIndex = 0, insertInt = 0;
         StringBuilder sB = new StringBuilder ();
         for (int i = 1; i <= nodeCount; i++) {
             for (int j = 0; j < nodeCharCount; j++) {
                 insertInt = Random.Range (0, 2); // 0 means we will insert an alphabet in our key code and 1 means we will go with a number
                 if (insertInt == 0) {
                     sB.Append (alpb [alpIndex]);
                     alpIndex++;
                     if (alpIndex == alpb.Length) {
                         alpIndex = 0;
                     }
                 } else {
                     sB.Append (num [numIndex]);
                     numIndex++;
                     if (numIndex == num.Length) {
                         numIndex = 0;
                     }
                 }
             }
             if (i < nodeCount) {
                 sB.Append ("-");
             }
         }
         return sB.ToString ();
     }
 
     static void shuffleArray<T>(T[] arr){ // This will not create a new array but return the original but shuffled array
         for (int i = arr.Length - 1; i > 0; i--) {
             int r = Random.Range (0, i + 1);
             T tmp = arr [i];
             arr [i] = arr [r];
             arr [r] = tmp;
         }
     }
 }
 
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 Zitoox · Oct 08, 2016 at 05:36 PM 0
Share

Thanks! It works fine =]

avatar image
1

Answer by Namey5 · Oct 08, 2016 at 07:59 AM

Random values are done in Unity through the included Random class. Here you would be looking at Random.Range.

 string Rand ()
 {
     int rand = Random.Range (0, 10);
     return rand.ToString ();
 }
 
 void Update ()
 {
     string code = Rand () + Rand () + Rand () + Rand () + "-" + Rand () + Rand () + Rand () + Rand () + "-" + Rand () + Rand () + Rand () + Rand ();
     Debug.Log (code);
 }

Something like that would generate a completely random code. You can find more about the Random class here;

https://docs.unity3d.com/ScriptReference/Random.html

https://docs.unity3d.com/ScriptReference/Random.Range.html

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
0

Answer by Bunny83 · Oct 08, 2016 at 09:27 AM

If you search for a self-verifying serial key that has the ability to expire at a certain date, have a look at my implementation over here

Of course self-verifying keys can be cracked quite easily. Make sure you read my other post carefully.

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

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Generate new blocks to land on 1 Answer

Generating script runtime 1 Answer

Making a bubble level (not a game but work tool) 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