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
1
Question by GEWLAR · Mar 06, 2014 at 03:27 PM · c#networknetworkviewviewid

Set NetworkViewID manually

We want to create a Multiplayer-Game with multiple Game Instances. In that case that every Game Instances needs to have its own NetworkViewID I have the following question:

Is it possible to set the NeworkViewID manually per script. I think about something like that:

 NetworkView.ViewID = 10;

Is this possible to do, because I don't like to use the Network.AllocateViewID() function. Ah btw I'm writing in C#.

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

2 Replies

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

Answer by stevethorne · Mar 06, 2014 at 03:31 PM

NetworkViewID's are the classes that store the information necessary for for a NetworkView. You can't set the number for this ViewID because Unity needs to make sure that the view is allocated and available. The issue that you think you're having is not an issue at all.

You are going to have multiple clients connecting to your server, that's what the NetworkViewID's are for! They'll work perfectly, but you need to know how to set them up properly. I'll explain the example from the link I posted in the comments to help you better understand how these work and how you can manipulate them.

 public Transform cubePrefab;
 void OnGUI() {
     if (GUILayout.Button("SpawnBox")) {
         NetworkViewID viewID = Network.AllocateViewID();
         networkView.RPC("SpawnBox", RPCMode.AllBuffered, viewID, transform.position);
     }
 }

First is this bit of code.

There is a button displayed on the GUI that says "SpawnBox" and, when clicked, it sends a message to every connected application. Let me tear this function call apart for you so you can better understand it.

"SpawnBox" = This is the RPC function that is going to be called.

RPCMode.AllBuffered = This means that it will be called on every connected application and it's buffered. This means that if the application connects after this function was called, it will call this function because it will be in the buffer and the new application needs to catch up.

The rest of the parameters are the parameters in the function that's going to be called.

So this function is sending the viewID that was just allocated by Unity and the position that they want the box spawned at.

 [RPC]
 void SpawnBox(NetworkViewID viewID, Vector3 location) {
     Transform clone;
     clone = Instantiate(cubePrefab, location, Quaternion.identity) as Transform as Transform;
     NetworkView nView;
     nView = clone.GetComponent<NetworkView>();
     nView.viewID = viewID;
 }

This is the function that's going to be called on every application. Note that it has the [RPC] attribute set for the function; this is needed for the function to be called through the RPC function. It's spawning a cube at the location provided from the application that sent the message and then assigns the viewID that was passed in from the other application to the NetworkView on the cube prefab.

This process makes sure that all the objects that are supposed to behave as the same object, have the same viewID on every client and the server.

I hope this clears up your confusion on the subject of NetworkViewIDs!

Comment
Add comment · Show 5 · 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 GEWLAR · Mar 06, 2014 at 03:39 PM 0
Share

I want to have fix ID's for my Game Instances that's why I don't want to use AllocateViewID.

How has the function to be to change the ID ?

avatar image stevethorne · Mar 06, 2014 at 04:01 PM 1
Share

I can't think of a reason why you would need fixed viewIDs, but what you wrote initially wont work.

avatar image stevethorne · Mar 06, 2014 at 04:04 PM 0
Share

Have you tried it yet? It might be that the only way you can use an ID is if it's received from AllocateViewID function. The function might do some other things to make that ID available for use. Take a look at this link if you're not sure how to use AllocateViewID properly. When you allocate the ID you can send it to every client that's running to let them know what the new one should be.

I imaging that you can use any ID that you want. But, you'll run the risk of using an ID for the wrong thing and have major issues. It's better if you use the function provided, but I don't think it's absolutely necessary.

avatar image GEWLAR · Mar 07, 2014 at 07:50 AM 0
Share

I want to have manually ID's because I have a game with multiple game instances on the Server. So i want to use my ID's becoause I have a logic to set them.

But If I use my written function it says: You can't convert type int into UnityEngine.NetworkViewID.

avatar image stevethorne · Mar 07, 2014 at 09:56 PM 0
Share

I updated the answer to explain to you how the NetworkViewID's work and why you can't set them to some number that you want.

avatar image
0

Answer by X28 · Jul 25, 2015 at 03:54 PM

In Editor : I Duplicate object that Contain networkView several Times. in each Duplication Unity Increase the ID number of ViewID. at last I Deleted the previous Object and Keep the New One. it has ID number different from 1.

this is a WAY. you can Choose another way ;)

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

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

Related Questions

Why is there an issue with Network recognition when networkView.isMine is called from within Update function ? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Player 2 not spawning 1 Answer

RPC called with a viewID, reaching server with a different viewID 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