playMaker

Author Topic: WWW POST  (Read 6153 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 13622
  • Official Playmaker Support
    • View Profile
WWW POST
« on: March 18, 2013, 05:44:58 AM »
Hi,

 Following a request, please find an extension of WWWObject that nows accept values to be posted with the url.

WARNING: I enabled textures, so that you can upload texture, BUT you need to enable the texture that you want to upload to be "Read/write", so go to the texture settings, select advanced and check "read/write", else you will get errors.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_FLASH || UNITY_PS3)

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Web")]
[Tooltip("Gets data from a url and store it in variables, Accept Post variables. See Unity WWW docs for more details.")]
public class WWWPOST : FsmStateAction
{
[RequiredField]
[Tooltip("Url to download data from.")]
public FsmString url;

[ActionSection("POST Data")]

[CompoundArray("POST", "Key", "Value")]
public FsmString[] postKeys;
public FsmVar[] postValues;

[ActionSection("Results")]

[UIHint(UIHint.Variable)]
[Tooltip("Gets text from the url.")]
public FsmString storeText;

[UIHint(UIHint.Variable)]
[Tooltip("Gets a Texture from the url.")]
public FsmTexture storeTexture;

        [UIHint(UIHint.Variable)]
[ObjectType(typeof(MovieTexture))]
[Tooltip("Gets a Texture from the url.")]
public FsmObject storeMovieTexture;

[UIHint(UIHint.Variable)]
[Tooltip("Error message if there was an error during the download.")]
public FsmString errorString;

[UIHint(UIHint.Variable)]
[Tooltip("How far the download progressed (0-1).")]
public FsmFloat progress;

[ActionSection("Events")]

[Tooltip("Event to send when the data has finished loading (progress = 1).")]
public FsmEvent isDone;

[Tooltip("Event to send if there was an error.")]
public FsmEvent isError;

private WWW wwwObject;

public override void Reset()
{
url = null;

postKeys = new FsmString[0];
postValues = new FsmVar[0];

storeText = null;
storeTexture = null;
errorString = null;
progress = null;
isDone = null;
}

public override void OnEnter()
{
if (string.IsNullOrEmpty(url.Value))
{
Finish();
return;
}

if (postKeys.Length>0)
{
WWWForm _wwwForm = new WWWForm();
int i = 0;

foreach(FsmString _Fsmkey in postKeys)
{
string _key = _Fsmkey.Value;

switch (postValues[i].Type)
{
case VariableType.Material:
case VariableType.Unknown:
case VariableType.Object:
//not supported;
break;
case VariableType.Texture:

Texture2D rt = (Texture2D)postValues[i].textureValue;

_wwwForm.AddBinaryData(_key,rt.EncodeToPNG());
break;
default:
_wwwForm.AddField(_key,postValues[i].ToString());
break;
}


i++;
}

wwwObject = new WWW(url.Value,_wwwForm);

}else{
wwwObject = new WWW(url.Value);
}
}


public override void OnUpdate()
{
if (wwwObject == null)
{
errorString.Value = "WWW Object is Null!";
Finish();
Fsm.Event(isError);
return;
}

errorString.Value = wwwObject.error;

if (!string.IsNullOrEmpty(wwwObject.error))
{
Finish();
Fsm.Event(isError);
return;
}

progress.Value = wwwObject.progress;

if (progress.Value.Equals(1f))
{
storeText.Value = wwwObject.text;
storeTexture.Value = wwwObject.texture;

                storeMovieTexture.Value = wwwObject.movie;

errorString.Value = wwwObject.error;

Fsm.Event(string.IsNullOrEmpty(errorString.Value) ? isDone : isError);

Finish();
}
}

public override string ErrorCheck ()
{
foreach(FsmVar _Fsmvar in postValues)
{
switch (_Fsmvar.Type)
{
case VariableType.Material:
case VariableType.Unknown:
case VariableType.Object:
return _Fsmvar.Type+" not supported";
break;
}
}
return "";
}


}
}

#endif

Bye,

 Jean

Sjones

  • Full Member
  • ***
  • Posts: 201
    • View Profile
Re: WWW POST
« Reply #1 on: June 05, 2013, 06:51:11 AM »
Hi Jean, sorry to dig up an old post, just curious to why the script has been left out of the mobile device builds?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 13622
  • Official Playmaker Support
    • View Profile
Re: WWW POST
« Reply #2 on: June 05, 2013, 10:32:55 PM »
Hi,

 because you would get the following error:

error CS1061: Type `UnityEngine.WWW' does not contain a definition for `movie' and no extension method `movie' of type `UnityEngine.WWW' could be found (are you missing a using directive or an assembly reference?)

So basically, if you want to use WWWPost, simply remove any reference to it. I have attached a mobile version.

Bye,

 Jean

Sjones

  • Full Member
  • ***
  • Posts: 201
    • View Profile
Re: WWW POST
« Reply #3 on: June 06, 2013, 12:32:35 PM »
Thanks!
Would it not be similar for non-pro versions too, thought video playback was limited to pro?
Either way that script will help us all :D

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 13622
  • Official Playmaker Support
    • View Profile
Re: WWW POST
« Reply #4 on: June 06, 2013, 09:56:22 PM »
Hi,

 I think you are right, it's mixture, but pro feature would not create compile error.

bye,

 Jean

unclebob

  • Playmaker Newbie
  • *
  • Posts: 25
    • View Profile
Re: WWW POST
« Reply #5 on: April 12, 2014, 01:37:41 AM »
Is it possible to capture the whole URL of a web page rendered on an in game object and save it to a FSM variable using this action?

Cheers

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 13622
  • Official Playmaker Support
    • View Profile
Re: WWW POST
« Reply #6 on: April 14, 2014, 05:17:44 AM »
Hi,

 no, you actuall provide the URL yourself.

 now, if the page is a server script and returns a plain text that is an url you want to use later, then yes. I do that all the time. But if your link is within a html document, then you'll get the whole document and you will have to make a regex search or something to get the data you need within that html page.


bye,

 JEan

Slater

  • Full Member
  • ***
  • Posts: 123
    • View Profile
Re: WWW POST
« Reply #7 on: May 26, 2014, 05:06:03 PM »
Do you have any info on how to use the WWWPOSTMobile?

The apps we are making is free from ads and in-app purchase etc since our target are kids. However when we release a new app we wan't to let the previous customers/buyers know that we have a new app out. We also sometimes run competitions or price reductions that would be nice to promote within our own apps to be able to reach out to our customers through cross promoting.

Instead of having to send in an updated version with the new info every time we want to change the message I was thinking that we should use the https://docs.unity3d.com/Documentation/ScriptReference/WWW.html and access items on a server we have set up. In that way we would be able to replace the content when a competition ends and replace with something else and easily make it change.

What I was thinking of having is like a screen after we show our company logo pop up with a fullsize texture/image covering the whole screen with the info we want to promote at the time. Also have an audio/MP3 play, reading the info for the kid/parent.

Would I be able to use this action in order to achieve this?

URL, I guess is where I will put in the www adress to the server?
POST Data POST, what is this?
Store Texture, does this mean "get the texture from the server and store it in a texture variable"?
Is there a way to do that as well for audio?

Do you have any suggestions on how to set this up?
I downloaded another action "Check for internet" that I think could be useful here as well.

FSM - Check for internet. If Off - play content, if On - do WWW.Post. Check if an Int has changed maybe that I get from server, if not changed - play content, if changed - download and save texture and audio in variable. Then set texture and set audio. Then play content.

What do I need on the server side? Do I need an SQL database or what do I store the texture and mp3 file as/in?

Another aspect I am a bit worried about is if someone would hack the server and change the picture to a pornographic or other crazy thing. Any suggestion on security strategy to prevent this?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 13622
  • Official Playmaker Support
    • View Profile
Re: WWW POST
« Reply #8 on: May 27, 2014, 04:14:22 AM »
Hi,

 -- I just added the WWWObjectRelative on the ecosystem that has an option to save content from the url as an audio.

Get the ecosystem browser:
https://hutonggames.fogbugz.com/default.asp?W1181

and search for WWW, then get the one, and test if that works for you.

POST is a way to send data to this url, so if your url is a scripted file it will understand POST. The developer that will create this server will know about this.

It depends your needs, but a php environemnt is likely the best approach, and you can save files on servers and serve them either directly ( direct url to the file) or via a php file ( a url as well, but running php), that in turns serve your content.

 I suggest php, but if you have no idea what POST is, I think you will struggle to get up and running with this...

If someone hack your server, porn thumbails is the least of your worry... If you are concerned about this, you should hire an expert, someone with experience on setting up servers and running a php/mysql environment. It's not difficult, but you need to have done it at least fea times.

also, DON'T host it yourself, just subscribe to a shared hosting service and your potential for hack will be vastly reduced, with only your php and file system being at risk if you don't set it and work it out properly.

 The easiest and most secure way here would be to use Parse, but if you start having lots of traffic then the price tag may be too much.

 Bye,


 Jean

Slater

  • Full Member
  • ***
  • Posts: 123
    • View Profile
Re: WWW POST
« Reply #9 on: May 27, 2014, 05:18:33 AM »
Hi,

Thank you for the reply. I know a bit about POST and PHP, I am studying that right now, but also have friends that are really good at that. I think I can get it working.

However I installed the ecosystem and have installed the WWWObjectRelative through that and it says it is installed and I see it in the customs actions folder, but it doesn't show up in the action browser.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 13622
  • Official Playmaker Support
    • View Profile
Re: WWW POST
« Reply #10 on: June 03, 2014, 05:09:51 AM »
Hi,

 is it sorted? cause I tested again and it's working. You have to wait for Unity to import the script, AND there must be no errors in the console for PlayMaker to process the script as an action.

Bye,

 Jean

Slater

  • Full Member
  • ***
  • Posts: 123
    • View Profile
Re: WWW POST
« Reply #11 on: June 07, 2014, 08:35:39 AM »
No it doesn't work because I need this for Mobile, iOS/Android and this action, WWWObjectRelative is not for Mobile like WWWPostMobile is.

Slater

  • Full Member
  • ***
  • Posts: 123
    • View Profile
Re: WWW POST
« Reply #12 on: June 19, 2014, 03:52:22 PM »
So, this doesn't work for Playmaker?

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3318
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: WWW POST
« Reply #13 on: June 19, 2014, 06:30:29 PM »
I think in the past WWW wasn't supported on iOS, so you see this at the top:
#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_FLASH || UNITY_PS3)

I think WWW does work with iOS now, so try deleting UNITY_IPHONE from that list:
#if !(UNITY_ANDROID || UNITY_FLASH || UNITY_PS3)

What is WWWPostMobile? Do you have a link?

Slater

  • Full Member
  • ***
  • Posts: 123
    • View Profile
Re: WWW POST
« Reply #14 on: June 21, 2014, 08:38:01 AM »
Ehm. Can't find it anymore on the forum. It was here before. If use check in the Ecosystem and search for www, you can see it there.